class MutexTypeD extends Mutex { private volatile int turn; private volatile boolean[] flag; MutexTypeD() { turn = 0; flag = new boolean[2]; flag[0] = flag[1] = false; } public void p(int id) { int other = 1 - id; flag[id] = true; while (flag[other] == true) { if (turn == other) { flag[id] = false; while (turn == other) Thread.yield(); flag[id] = true; } } } public void v(int id) { turn = 1 - id; flag[id] = false; } public String toString() { return "D"; } public void describe() { System.out.println("Mutex type D"); } }