1 17 18 package org.objectweb.jac.util; 19 20 import org.apache.log4j.Logger; 21 22 23 24 29 30 public class Semaphore { 31 static Logger logger = Logger.getLogger("semaphore"); 32 33 34 protected int count = 0; 35 private int waitingCount = 0; 36 37 44 45 public Semaphore(int n) { 46 this.count = n; 47 } 48 49 63 64 public Semaphore() { 65 } 66 67 76 77 public int getWaitingCount() { 78 return waitingCount; 79 } 80 81 public int getCount() { 82 return count; 83 } 84 85 97 public boolean acquire() { 98 return acquire(0); 99 } 100 101 114 public synchronized boolean acquire(long timeout) { 115 waitingCount++; 116 while(count == 0) { 117 logger.debug(this+" acquire, waiting; count = "+count); 118 try { 119 wait(timeout); 120 if (count==0) 123 return false; 124 } catch (InterruptedException e) { 125 } 127 } 128 logger.debug(this+" acquired, count = "+count+"(--)"); 129 count--; 130 waitingCount--; 131 return true; 132 } 133 134 145 public synchronized void release() { 146 logger.debug(this+" release, count = "+count+"(++)"); 147 count++; 148 notify(); } 150 } 151 | Popular Tags |