1 package org.columba.core.base; 17 18 30 public final class Semaphore { 31 32 private Object lockObject = new Object (); 33 34 private boolean isReleased = false; 35 36 39 public Semaphore() { 40 this(true); 41 } 42 43 49 public Semaphore(boolean isHolding) { 50 if (isHolding) { 51 hold(); 52 } else { 53 release(); 54 } 55 } 56 57 63 public void hold() { 64 isReleased = false; 65 } 66 67 75 public void waitUntilReleased() throws InterruptedException { 76 waitUntilReleased(0); 77 } 78 79 87 public void waitUntilReleased(long timeout) throws InterruptedException { 88 if (!isReleased) { 89 synchronized (lockObject) { 90 lockObject.wait(timeout); 91 } 92 } 93 } 94 95 98 public void release() { 99 isReleased = true; 100 synchronized (lockObject) { 101 lockObject.notifyAll(); 102 } 103 } 104 105 111 public boolean isHolding() { 112 return !isReleased; 113 } 114 } 115 | Popular Tags |