Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.
1 11 package org.eclipse.core.internal.jobs; 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 26 public synchronized boolean acquire(long delay) throws InterruptedException { 27 if (Thread.interrupted()) 28 throw new InterruptedException (); 29 long start = System.currentTimeMillis(); 30 long timeLeft = delay; 31 while (true) { 32 if (notifications > 0) { 33 notifications--; 34 return true; 35 } 36 if (timeLeft <= 0) 37 return false; 38 wait(timeLeft); 39 timeLeft = start + delay - System.currentTimeMillis(); 40 } 41 } 42 43 public boolean equals(Object obj) { 44 return (runnable == ((Semaphore) obj).runnable); 45 } 46 47 public int hashCode() { 48 return runnable == null ? 0 : runnable.hashCode(); 49 } 50 51 public synchronized void release() { 52 notifications++; 53 notifyAll(); 54 } 55 56 public String toString() { 58 return "Semaphore(" + runnable + ")"; } 60 } 61
| Popular Tags
|