1 11 package org.eclipse.core.internal.utils; 12 13 public class Semaphore { 14 protected long notifications; 15 protected Runnable runnable; 16 17 public Semaphore(Runnable runnable) { 18 this.runnable = runnable; 19 notifications = 0; 20 } 21 22 public synchronized void acquire() throws InterruptedException { 23 if (Thread.interrupted()) 24 throw new InterruptedException (); 25 while (notifications <= 0) 26 wait(); 27 notifications--; 28 } 29 30 public boolean equals(Object obj) { 31 return (runnable == ((Semaphore) obj).runnable); 32 } 33 34 public Runnable getRunnable() { 35 return runnable; 36 } 37 38 public int hashCode() { 39 return runnable.hashCode(); 40 } 41 42 public synchronized void release() { 43 notifications++; 44 notifyAll(); 45 } 46 47 public String toString() { 49 return String.valueOf(runnable); 50 } 51 } | Popular Tags |