12345678910111213141516171819202122232425262728293031 |
- class MutexTypeH extends Mutex {
- private volatile int turn;
- private volatile boolean[] flag;
-
- MutexTypeH() {
- turn = 0;
- flag = new boolean[2];
- flag[0] = flag[1] = false;
- }
- public void p(int id) {
- int other = 1 - id;
- flag[id] = true;
- while (turn == other) {
- while (flag[other]) Thread.yield();
- turn = id;
- }
- }
- public void v(int id) {
- flag[id] = false;
- }
- public String toString() {
- return "H";
- }
- }
|