1 3 package jodd.util; 4 5 15 public class Mutex { 16 17 private Thread owner = null; 18 19 23 public synchronized void lock() { 24 Thread currentThread = Thread.currentThread(); 25 if (owner == currentThread) { 26 return; 27 } 28 while (owner != null) { 29 try { 30 wait(); 31 } catch (InterruptedException iex) { 32 notify(); 33 } 34 } 35 owner = currentThread; 36 } 37 38 41 public synchronized boolean tryLock() { 42 Thread currentThread = Thread.currentThread(); 43 if (owner == currentThread) { 44 return true; 45 } 46 if (owner != null) { 47 return false; 48 } 49 owner = currentThread; 50 return true; 51 } 52 53 56 public synchronized void unlock() { 57 owner = null; 58 notify(); 59 } 60 } 61 | Popular Tags |