-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPortArray_Test.java
144 lines (126 loc) · 3.97 KB
/
PortArray_Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class ThreadProducer<T> extends Thread {
PortArray<Integer> prts;
SynchPort<Integer> prt;
int portsSize;
long waitingMillis;
int id;
int msgNum;
public ThreadProducer(PortArray prts, SynchPort prt, int portsSize, int id, int waitingMillis)
{
this.id = id;
this.prts = prts;
this.prt = prt;
this.portsSize = portsSize;
this.waitingMillis = waitingMillis;
}
public void run() {
Message<Integer> m;
try {
for (int j=0; j<portsSize; j++){
System.out.println("\tProduttore " + this.id + " aspetta di inviare alla porta "+ j);
try {
Thread.sleep(waitingMillis);
} catch (InterruptedException ie) {
System.err.println("InterruptedException sleeping");
}
m = new Message<Integer> (this.id, prt);
System.out.println("\t\tProduttore " + this.id + " invia alla porta "+ j);
prts.send(m, j);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class ThreadConsumer<T> extends Thread {
PortArray<Integer> prts;
int portsSize;
int id;
int v[];
int n;
int waitingMillis;
public ThreadConsumer(PortArray prts, int portsSize, int id, int array[], int dim, int waitingMillis)
{
this.id = id;
this.prts = prts;
this.portsSize = portsSize;
this.v = array;
this.n = dim;
this.waitingMillis = waitingMillis;
}
public void run() {
try {
System.out.println("Consumatore " + this.id + " ---> aspetta di ricevere");
try {
Thread.sleep(waitingMillis);
} catch (InterruptedException ie) {
System.err.println("InterruptedException sleeping");
}
for(int i = 0; i < n*2; i++){
Msg_Rcv m = prts.receive(v,v.length);
System.out.println("Consumatore " + this.id + " ---> riceve sulla porta "+ m.num_port + " messaggio dal produttore " + m.message.data);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class PortArray_Test {
private static PortArray<Integer> prConsumer;
private static SynchPort<Integer> prProducer1;
private static SynchPort<Integer> prProducer2;
private static int portsSize;
public static void main (String[] args) {
System.out.println("Main: starting \n");
portsSize = 3;
PortArray<Integer> ports = new PortArray<Integer>(portsSize);
prConsumer = new PortArray<Integer>(portsSize);
prProducer1 = new SynchPort<Integer>();
prProducer2 = new SynchPort<Integer>();
int z[] = {1};
int w[] = {0, 2};
int v[] = {2, 0, 1};
int n = 0;
int m = 0;
int array[];
int array2[] = null;
double random = Math.random();
if(random > 0.5){
System.out.println("--------------Comportamento previsto Consumer--------------");
System.out.println("\nSingolo Consumer che effettua la lettura su tutte le porte \n");
System.out.println("-----------------------------------------------------------\n");
array = v;
n = v.length;
} else{
System.out.println("--------------Comportamento previsto Consumer--------------");
System.out.println("\n Due Consumer che effettua la lettura su porte differenti");
System.out.println("--> Consumer 0: legge le porte 0, 2");
System.out.println("--> Consumer 1: legge la porta 1\n");
System.out.println("-----------------------------------------------------------\n");
array = w;
array2 = z;
n = w.length;
m = z.length;
}
ThreadConsumer<Integer> cons = new ThreadConsumer<Integer>(prConsumer, portsSize, 0, array, n, 100);
ThreadConsumer<Integer> cons1 = null;
if(m > 0){
cons1 = new ThreadConsumer<Integer>(prConsumer, portsSize, 1, array2, m, 100);
}
ThreadProducer<Integer> prod1 = new ThreadProducer<Integer>(prConsumer, prProducer1, portsSize, 1, 50);
ThreadProducer<Integer> prod2 = new ThreadProducer<Integer>(prConsumer, prProducer1, portsSize, 2, 70);
cons.start();
if(m > 0) cons1.start();
prod1.start();
prod2.start();
try {
cons.join();
if(m > 0) cons1.join();
prod1.join();
prod2.join();
} catch (InterruptedException ie) {
System.err.println("InterruptedException joining");
}
System.out.println("Main: finished");
}
}