public class Semaphore implements java.io.Serializable { private static final long serialVersionUID = -3222578661600680210L; /** All mechanics via AbstractQueuedSynchronizer subclass */ private final Sync sync; /** * 繼承AQS以實現信號量機制,使用AQS的state屬性表明容許使用的信號量的數量,該Sync類有兩個 * 派生類,其中一個是公平模式的,另外一個是非公平模式的 */ abstract static class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 1192457210091910933L; Sync(int permits) { setState(permits); } final int getPermits() { return getState(); } //非公平模式的獲取信號量 final int nonfairTryAcquireShared(int acquires) { for (;;) { int available = getState(); int remaining = available - acquires; if (remaining < 0 || compareAndSetState(available, remaining)) return remaining; } } //釋放信號量 protected final boolean tryReleaseShared(int releases) { for (;;) { int current = getState(); int next = current + releases; if (next < current) // overflow throw new Error("Maximum permit count exceeded"); if (compareAndSetState(current, next)) return true; } } //減小信號量 final void reducePermits(int reductions) { for (;;) { int current = getState(); int next = current - reductions; if (next > current) // underflow throw new Error("Permit count underflow"); if (compareAndSetState(current, next)) return; } } 清空全部信號量 final int drainPermits() { for (;;) { int current = getState(); if (current == 0 || compareAndSetState(current, 0)) return current; } } } }
public class Semaphore implements java.io.Serializable { /** * 非公平模式 */ static final class NonfairSync extends Sync { private static final long serialVersionUID = -2694183684443567898L; NonfairSync(int permits) { super(permits); } protected int tryAcquireShared(int acquires) { return nonfairTryAcquireShared(acquires); } } /** * 公平模式 */ static final class FairSync extends Sync { private static final long serialVersionUID = 2014338818796000944L; FairSync(int permits) { super(permits); } protected int tryAcquireShared(int acquires) { for (;;) { //公平模式下,先判斷是否有前驅結點 if (hasQueuedPredecessors()) return -1; int available = getState(); int remaining = available - acquires; if (remaining < 0 || compareAndSetState(available, remaining)) return remaining; } } } }
public class Semaphore implements java.io.Serializable { //未指定模式,默認生產非公平模式的信號量工具 public Semaphore(int permits) { sync = new NonfairSync(permits); } //指定模式,生產公平或非公平模式的信號量工具 public Semaphore(int permits, boolean fair) { sync = fair ? new FairSync(permits) : new NonfairSync(permits); } //使用可中斷的方式獲取信號量,tryAcquireShared->doAcquireSharedInterruptibly public void acquire() throws InterruptedException { sync.acquireSharedInterruptibly(1); } //發起可中斷的獲取信號量,指定獲取信號量的數量 public void acquire(int permits) throws InterruptedException { if (permits < 0) throw new IllegalArgumentException(); sync.acquireSharedInterruptibly(permits); } //使用不可中斷的方式獲取信號量,tryAcquireShared->doAcquireShared public void acquireUninterruptibly() { sync.acquireShared(1); } //發起不可中的的獲取信號量,指定獲取信號量的數量 public void acquireUninterruptibly(int permits) { if (permits < 0) throw new IllegalArgumentException(); sync.acquireShared(permits); } //使用非公平模式獲取信號量,默認獲取一個信號量 public boolean tryAcquire() { return sync.nonfairTryAcquireShared(1) >= 0; } //使用非公平模式獲取信號量,指定獲取信號量的數量 public boolean tryAcquire(int permits) { if (permits < 0) throw new IllegalArgumentException(); return sync.nonfairTryAcquireShared(permits) >= 0; } //使用超時的模式獲取信號量,默認獲取一個信號量 public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } //使用超時的模式獲取信號量,指定獲取信號量的數量 public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException { if (permits < 0) throw new IllegalArgumentException(); return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout)); } //釋放信號量 public void release() { sync.releaseShared(1); } }