-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
73 lines (58 loc) · 1.49 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
public class Main {
private static void withTime(Time t){
var A = t.freshThread(() -> {
t.sleep(2000);
System.out.println(" A");
});
var B = t.freshThread(() -> {
for(int i = 0; i < 4; i++){
t.sleep(499);
System.out.println(" B");
}
});
A.start();
B.start();
try {
A.join();
B.join();
} catch (Exception e) {}
}
private static void withRendevouz(Time t){
var semA = t.freshSemaphore(0);
var semB = t.freshSemaphore(0);
var A = t.freshThread(() -> {
for (int i = 0; i < 4; i++) {
t.sleep(1000);
System.out.println(" A");
semB.release();
semA.acquire();
}
});
var B = t.freshThread(() -> {
for (int i = 0; i < 4; i++) {
t.sleep(499);
System.out.println(" B");
semA.release();
semB.acquire();
}
});
A.start();
B.start();
try {
A.join();
B.join();
} catch (Exception e) {}
}
public static void main(String[] args) {
System.out.println("Running in real time: ");
withTime(new Concrete());
System.out.println("Running in virtual time no jitter: ");
withTime(new Virtual(0));
System.out.println("Running in virtual time 5ms jitter: ");
withTime(new Virtual(5));
System.out.println("Rendezvous in real time: ");
withRendevouz(new Concrete());
System.out.println("Rendezvous in virtual time no jitter: ");
withRendevouz(new Virtual(0));
}
}