01 /*
02 Exclusion Mutuelle :
03 */
04
05 public class Mutex extends ExclusionMutuelle
06 {
07 public Mutex() {
08 flag[0] = false;
09 flag[1] = false;
10 turn = 0;
11 }
12
13 public void Pmutex(int t) {
14 int other = 1−t;
15 flag[t]= true;
16
17 while (turn==other) {
18 if (flag[other])
19 Thread.yield();
20 if (flag[t])
21 turn = t;
22 }
23 }
24
25 public void Vmutex(int t) {
26 flag[t] = false;
27 }
28
29 private volatile int turn;
30 private volatile boolean[] flag = new boolean[2];
|