1234567891011121314151617181920212223242526272829303132333435 |
- 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";
- }
- public void describe() {
- System.out.println("Mutex type H");
- }
- }
|