-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailbox_A_Test.java
79 lines (66 loc) · 1.9 KB
/
Mailbox_A_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
import java.util.LinkedList;
import java.util.Queue;
class Producer_A extends Thread {
private Mailbox_A server;
private SynchPort<Integer> myPort;
//public Producer(Mailbox server,int priority) {
public Producer_A(Mailbox_A server) {
this.server = server;
myPort = new SynchPort<Integer>();
}
public void run() {
try {
for (int i = 0; i < 5; i++) {
server.request_to_insert(myPort);
myPort.send(new Message<Integer>(i, null));
}
} catch (InterruptedException e) {
System.out.println("Exception found on producer"+Thread.currentThread().getId());
}
}
}
class Consumer_A extends Thread {
private Mailbox_A server;
private SynchPort<Integer> myPort;
public Queue<Boolean> testingConsistency;
public Consumer_A(Mailbox_A server) {
this.server = server;
myPort = new SynchPort<Integer>();
}
public void run() {
try {
for (int i = 0; i < 50; i++) {
server.request_to_remove(myPort);
Message<Integer> msg = myPort.receive();
System.out.println("Messagio ricevuto dal thread: "+ msg.getIdThread()+". Valore: "+ msg.getData());
Thread.sleep(100);
}
} catch (InterruptedException e) {
System.out.println("Exception found on producer"+Thread.currentThread().getId());
}
}
}
public class Mailbox_A_Test{
public static void main(String[] args) throws InterruptedException {
System.out.println("Inizio");
// create a new server
Mailbox_A server = new Mailbox_A();
// server starts
server.start();
// create and start 10 producer
Producer_A[] producers = new Producer_A[10];
for (int i = 0; i < 10; i++) {
producers[i] = new Producer_A(server);
producers[i].start();
}
// create and start the consumer
System.out.println("Consumer Start");
Consumer_A consumer = new Consumer_A(server);
consumer.start();
// join all
for (int i = 0; i < 10; i++)
producers[i].join();
consumer.join();
System.out.println("Fine");
}
}