In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "JUC Semaphore source code example analysis", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "JUC Semaphore source code example analysis" this article.
Semaphore is mainly used to limit the number of utility classes that control the concurrent execution of code, which defines the amount of concurrent execution through a permit.
/ * use unfair 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);} / * the subclass of AQS mainly defines getting and releasing lock * / abstract static class Sync extends KAbstractQueuedSynchronizer {private static final long serialVersionUID = 1192457210091910933L; / * specify permit initialization Semaphore * / Sync (int permits) {setState (permits);} / * return remaining permit * / final int getPermits () {return getState ();} / * get permit * / final int nonfairTryAcquireShared (int acquires) {for () ;) {int available = getState (); int remaining = available-acquires; / / determine the number of remaining permit to get acquires if (remaining
< 0 || compareAndSetState(available, remaining)){ // cas改变 state return remaining; } } } /** * 释放 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改变 state return true; } } } final void reducePermits(int reductions){ // 减少 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 get permit * / public void acquire () throws InterruptedException {sync.acquireSharedInterruptibly (1);} / * call acquireUninterruptibly to get permit * / public void acquireUninterruptibly () {sync.acquireShared (1);} / * attempt to get permit * / public boolean tryAcquire () {return sync.nonfairTryAcquireShared (1) > = 0 Permit attempted to support timeout and interruption * / public boolean tryAcquire (long timeout, TimeUnit unit) throws InterruptedException {return sync.tryAcquireSharedNanos (1, unit.toNanos (timeout);} / * support interrupt acquisition permit * / public void acquire (int permits) throws InterruptedException {if (permits)
< 0){ throw new IllegalArgumentException(); } sync.acquireSharedInterruptibly(permits);}/** * 不响应中断的获取 permit */public void acquireUninterruptibly(int permits){ if(permits < 0) throw new IllegalArgumentException(); sync.acquireShared(permits);}/** * 尝试获取 permit */public boolean tryAcquire(int permits){ if(permits < 0) throw new IllegalArgumentException(); return sync.nonfairTryAcquireShared(permits) >= 0;} / * attempt to support timeout mechanism, support interrupt acquisition 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 ();} / * depleted permit * / 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 waiting thread in Sync Queue in AQS * / public final boolean hasQueuedThreads () {return sync.hasQueuedThreads ();} / * return waiting thread length in Sync Queue in AQS * / public final int getQueueLength () {return sync.getQueueLength ();} / * return waiting thread in Sync Queue in AQS * / protected Collection getQueueThreads () {return sync.getQueuedThreads () } the above is all the contents of this article "sample Analysis of Semaphore Source Code of JUC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.