Semaphore is mainly used to limit the number of tool classes that control concurrent code execution, and internally defines the number of concurrent executions through a permit.
/** * Use non-fair version component Semaphore */public KSemaphore(int permits){ sync = new NonfairSync(permits);}/** * Specify version component Semaphore */public KSemaphore(int permits, boolean fair){ sync = fair ? new FairSync(permits) : new NonfairSync(permits);} /** AQS subclass main definition get release lock */abstract static class Sync extends KAbstractQueuedSynchronizer{ private static final long serialVersionUID = 1192457210091910933L; /** * Specify permission Initialize Semaphore */ Sync(int permits){ setState(permits); } /** * Return the remaining permit */ final int getPermits(){ return getState(); } /** * Get permission */ final int nonfairTryAcquireShared(int acquires){ for(;;){ int available = getState(); int remaining = available - acquires; // determine the number of remaining permits to obtain acquires if(remaining < 0 || compareAndSetState(available, remaining)){ // cas change state return remaining; } } } } /** * Release lock */ 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)){ // cas change state return true; } } } final void reducePermits(int reductions){ // reduce permits for(;;){ int current = getState(); int next = current - reductions; if(next > current){ // underflow throw new Error(" Permit count underflow "); } if(compareAndSetState(current, next)){ return; } } } } /** Set permit to 0 */ final int drainPermits(){ for(;;){ int current = getState(); if(current == 0 || compareAndSetState(current, 0)){ return current; } } }} /** * Call acquireSharedInterruptibly to obtain permission in response to interrupt */public void acquire() throws InterruptedException{ sync.acquireSharedInterruptibly(1);}/** * Call acquireUninterruptibly to obtain permission in non-responsive interrupt */public void acquireUninterruptibly(){ sync.acquireShared(1);}/** * Try to obtain permission */public boolean tryAcquire(){ return sync.nonfairTryAcquireShared(1) >= 0;}/** * Try to obtain permission, support timeout and interrupt*/public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException{ return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));}/** * Support to obtain permission */public void acquire(int permits) throws InterruptedException{ if(permits < 0){ throw new IllegalArgumentException(); } sync.acquireSharedInterruptibly(permits);}/** * Get permission that does not respond to interrupts */public void acquireUninterruptibly(int permits){ if(permits < 0) throw new IllegalArgumentException(); sync.acquireShared(permits);}/** * Try to get permission */public boolean tryAcquire(int permits){ if(permits < 0) throw new IllegalArgumentException(); return sync.nonfairTryAcquireShared(permits) >= 0;}/** * Try to support the timeout mechanism, support the acquisition of interrupts permit */public boolean tryAcquire(int permits, long timout, TimeUnit unit) throws InterruptedException{ if(permits < 0) throw new IllegalArgumentException(); return sync.tryAcquireSharedNanos(permits, unit.toNanos(timout));} /** * Release permit */public void release(){ sync.releaseShared(1);}/** * Release permit */public void release(int permits){ if(permits < 0) throw new IllegalArgumentException(); sync.releaseShared(permits);} /** * Return available permit */public int availablePermits(){ return sync.getPermits();}/** * Consuming permission */public int drainPermits(){ return sync.drainPermits();}/** * Reduce reduction permit */protected void reducePermits(int reduction){ if(reduction < 0) throw new IllegalArgumentException(); sync.reducePermits(reduction);}/** * Determine whether it is a fair version*/public boolean isFair(){ return sync instanceof FairSync;}/** * Return the waiting thread in Sync Queue in AQS*/public final boolean hasQueuedThreads(){ return sync.hasQueuedThreads();}/** * Return the waiting thread length in Sync Queue in AQS*/public final int getQueueLength(){ return sync.getQueueLength();}/** * Return the waiting thread in Sync Queue in AQS*/protected Collection<Thread> getQueueThreads(){ return sync.getQueuedThreads();}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.