1 7 8 package java.util.concurrent; 9 import java.util.*; 10 import java.util.concurrent.locks.*; 11 import java.util.concurrent.atomic.*; 12 13 126 127 public class Semaphore implements java.io.Serializable { 128 private static final long serialVersionUID = -3222578661600680210L; 129 130 private final Sync sync; 131 132 137 abstract static class Sync extends AbstractQueuedSynchronizer { 138 Sync(int permits) { 139 setState(permits); 140 } 141 142 final int getPermits() { 143 return getState(); 144 } 145 146 final int nonfairTryAcquireShared(int acquires) { 147 for (;;) { 148 int available = getState(); 149 int remaining = available - acquires; 150 if (remaining < 0 || 151 compareAndSetState(available, remaining)) 152 return remaining; 153 } 154 } 155 156 protected final boolean tryReleaseShared(int releases) { 157 for (;;) { 158 int p = getState(); 159 if (compareAndSetState(p, p + releases)) 160 return true; 161 } 162 } 163 164 final void reducePermits(int reductions) { 165 for (;;) { 166 int current = getState(); 167 int next = current - reductions; 168 if (compareAndSetState(current, next)) 169 return; 170 } 171 } 172 173 final int drainPermits() { 174 for (;;) { 175 int current = getState(); 176 if (current == 0 || compareAndSetState(current, 0)) 177 return current; 178 } 179 } 180 } 181 182 185 final static class NonfairSync extends Sync { 186 NonfairSync(int permits) { 187 super(permits); 188 } 189 190 protected int tryAcquireShared(int acquires) { 191 return nonfairTryAcquireShared(acquires); 192 } 193 } 194 195 198 final static class FairSync extends Sync { 199 FairSync(int permits) { 200 super(permits); 201 } 202 203 protected int tryAcquireShared(int acquires) { 204 Thread current = Thread.currentThread(); 205 for (;;) { 206 Thread first = getFirstQueuedThread(); 207 if (first != null && first != current) 208 return -1; 209 int available = getState(); 210 int remaining = available - acquires; 211 if (remaining < 0 || 212 compareAndSetState(available, remaining)) 213 return remaining; 214 } 215 } 216 } 217 218 225 public Semaphore(int permits) { 226 sync = new NonfairSync(permits); 227 } 228 229 238 public Semaphore(int permits, boolean fair) { 239 sync = (fair)? new FairSync(permits) : new NonfairSync(permits); 240 } 241 242 271 public void acquire() throws InterruptedException { 272 sync.acquireSharedInterruptibly(1); 273 } 274 275 294 public void acquireUninterruptibly() { 295 sync.acquireShared(1); 296 } 297 298 321 public boolean tryAcquire() { 322 return sync.nonfairTryAcquireShared(1) >= 0; 323 } 324 325 366 public boolean tryAcquire(long timeout, TimeUnit unit) 367 throws InterruptedException { 368 return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); 369 } 370 371 383 public void release() { 384 sync.releaseShared(1); 385 } 386 387 426 public void acquire(int permits) throws InterruptedException { 427 if (permits < 0) throw new IllegalArgumentException (); 428 sync.acquireSharedInterruptibly(permits); 429 } 430 431 455 public void acquireUninterruptibly(int permits) { 456 if (permits < 0) throw new IllegalArgumentException (); 457 sync.acquireShared(permits); 458 } 459 460 488 public boolean tryAcquire(int permits) { 489 if (permits < 0) throw new IllegalArgumentException (); 490 return sync.nonfairTryAcquireShared(permits) >= 0; 491 } 492 493 544 public boolean tryAcquire(int permits, long timeout, TimeUnit unit) 545 throws InterruptedException { 546 if (permits < 0) throw new IllegalArgumentException (); 547 return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout)); 548 } 549 550 571 public void release(int permits) { 572 if (permits < 0) throw new IllegalArgumentException (); 573 sync.releaseShared(permits); 574 } 575 576 581 public int availablePermits() { 582 return sync.getPermits(); 583 } 584 585 589 public int drainPermits() { 590 return sync.drainPermits(); 591 } 592 593 602 protected void reducePermits(int reduction) { 603 if (reduction < 0) throw new IllegalArgumentException (); 604 sync.reducePermits(reduction); 605 } 606 607 611 public boolean isFair() { 612 return sync instanceof FairSync; 613 } 614 615 625 public final boolean hasQueuedThreads() { 626 return sync.hasQueuedThreads(); 627 } 628 629 638 public final int getQueueLength() { 639 return sync.getQueueLength(); 640 } 641 642 652 protected Collection<Thread > getQueuedThreads() { 653 return sync.getQueuedThreads(); 654 } 655 656 663 public String toString() { 664 return super.toString() + "[Permits = " + sync.getPermits() + "]"; 665 } 666 667 } 668 | Popular Tags |