-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
81 lines (69 loc) · 2.14 KB
/
Main.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
import java.util.concurrent.Semaphore;
public class Main {
private static Semaphore mutex = new Semaphore(1);
private static BufferQueuee SR = new BufferQueuee(20, true);
static int c = 0;
public static void main(String[] args) {
Sender s = new Sender(1);
Sender s2 = new Sender(2);
Reciever r = new Reciever(1);
Sender s3 = new Sender(3);
Sender s4 = new Sender(4);
// Reciever r1 = new Reciever(2);
// Reciever r2 = new Reciever(3);
// Reciever r3 = new Reciever(4);
s.start();
s4.start();
s2.start();
s3.start();
r.start();
// r1.start();
// r2.start();
// r3.start();
// s2.start();
}
static class Sender extends Thread {
int number;
public Sender(int number) {
this.number = number;
}
@Override
public void run() {
while (true) {
try {
mutex.acquire();
SR.send_msg("msg" + c + " S" + number);// PRODUCE
c++;
mutex.release();
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
static class Reciever extends Thread {
String msg;
int number;
public Reciever(int number) {
this.number = number;
}
@Override
public void run() {
while (true) {
long stats[] = SR.stats();
msg = SR.get_msgnb();
System.out
.println(msg + " R" + number + "\tmessages_length:" + stats[0] + "\tnumber_messages:" + stats[1]
+ "\tmemory usage:" + stats[2]); // CONSUME
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}