1 19 package org.netbeans.mdr.util; 20 21 import java.util.LinkedList ; 22 import java.util.WeakHashMap ; 23 24 29 public class RWMutex { 30 private Thread thread = null; 31 private int counter = 0; 32 private final RWMutex dependent; 33 34 35 public RWMutex() { 36 dependent = null; 37 } 38 39 42 public RWMutex(RWMutex dependent) { 43 this.dependent = dependent; 44 } 45 46 public synchronized void enter(boolean writer) { 47 Thread thread = Thread.currentThread(); 48 while (counter > 0 && this.thread != thread) { 49 try { 50 this.wait(); 51 } catch (InterruptedException e) { 52 Logger.getDefault().notify(Logger.INFORMATIONAL, e); 53 } 54 } 55 56 this.thread = thread; 57 counter++; 58 if (dependent != null) dependent.enter(false); 59 } 60 61 public synchronized void leave() { 62 if (dependent != null) dependent.leave(); 63 counter--; 64 this.notifyAll(); 65 } 66 } 67 | Popular Tags |