1 package jodd.util; 2 3 18 public class MutexSync { 19 20 private volatile Thread x; 21 private volatile Thread y; 22 private volatile Thread current_owner; 23 24 27 public MutexSync() { 28 this.y = null; 29 this.x = null; 30 this.current_owner = null; 31 } 32 33 public synchronized void lock() { 34 Thread _current = Thread.currentThread(); 35 while (true) { 36 this.x = _current; 37 if (this.y != null) { 38 _current.yield(); 39 continue; 40 } 41 this.y = _current; 42 if (this.x != _current) { 43 _current.yield(); 44 if (this.y != _current) { 45 _current.yield(); 46 continue; 47 } 48 } 49 this.current_owner = _current; 50 break; 51 } 52 } 53 54 60 public synchronized boolean lockTry() { 61 if (isLocked() == true) { 62 return false; 63 } 64 lock(); 65 return true; 66 } 67 68 public void unlock() { 69 Thread _current = Thread.currentThread(); 70 this.current_owner = null; 71 this.y = null; } 73 74 public boolean isLocked() { 75 return (null != this.y); 76 } 77 78 } 79 | Popular Tags |