-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
72 lines (63 loc) · 1.84 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
// Dekker's algorithm is the first known correct solution
// to the mutual exclusion problem in concurrent
// programming. The solution is attributed to Dutch
// mathematician Th. J. Dekker by Edsger W. Dijkstra.
// It allows two threads to share a single-use resource
// without conflict, using only shared memory for
// communication.
class Main {
static boolean[] flag = {false, false};
static int turn = 0;
static int N = 4;
// flag: ith process wants to enter CS?
// turn: whose turn to enter CS
// N: number of loops
// 1. I want to enter CS.
// 2. If you want CS too ...
// 3a. If its my turn, retry 2.
// 4a. If its your turn, i dont want to enter.
// 4b. I wait for you turn to complete.
// 4c. I now want to enter, retry 2.
// 5. I enter CS (sleep).
// 6. Its your turn now.
// 7. I dont want CS.
static Thread process(int i) {
return new Thread(() -> {
int j = 1 - i;
for (int n=0; n<N; n++) {
log(i+": want CS"); // LOCK
flag[i] = true; // 1
while (flag[j]) { // 2
if (turn == i) { Thread.yield(); continue; } // 3a
flag[i] = false; // 4a
while (turn == j) Thread.yield(); // 4b
flag[i] = true; // 4c
}
log(i+": in CS"+n);
sleep(1000 * Math.random()); // 5
log(i+": done CS"); // UNLOCK
turn = j; // 6
flag[i] = false; // 7
}
});
}
public static void main(String[] args) {
try {
log("Starting 2 processes (threads) ...");
Thread p0 = process(0);
Thread p1 = process(1);
p0.start();
p1.start();
p0.join();
p1.join();
}
catch (InterruptedException e) {}
}
static void sleep(double t) {
try { Thread.sleep((long)t); }
catch (InterruptedException e) {}
}
static void log(String x) {
System.out.println(x);
}
}