In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize the CountDownLatch of JUC". In the daily operation, I believe that many people have doubts about how to realize the CountDownLatch of JUC. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the CountDownLatch of JUC". Next, please follow the editor to study!
CountDownLatch is a synchronization aid that blocks one or more current threads to wait for operations in other threads to complete. When constructing the CountDownLatch object, we need to specify a non-negative count value. In general, the thread calling the CountDownLatch#await method needs to block and wait for the count value to change to 0 before it can proceed.
Insider implementation of CountDownLatch
CountDownLatch also relies on AQS components in its implementation. A Sync inner class is defined inside CountDownLatch, which inherits from AbstractQueuedSynchronizer and reuses the state field of AQS to record changes in count values. The core methods of CountDownLatch are delegated to Sync for processing, and the implementation is as follows:
Public class CountDownLatch {private final Sync sync; public CountDownLatch (int count) {if (count < 0) {throw new IllegalArgumentException ("count < 0");} this.sync = new Sync (count);} public void await () throws InterruptedException {sync.acquireSharedInterruptibly (1);} public boolean await (long timeout, TimeUnit unit) throws InterruptedException {return sync.tryAcquireSharedNanos (1, unit.toNanos (timeout)) } public void countDown () {sync.releaseShared (1);} public long getCount () {return sync.getCount ();}}
Let's focus on the implementation of the CountDownLatch#await and CountDownLatch#countDown methods.
First take a look at the CountDownLatch#await method, which is used to block the current thread until the count value changes to zero or is interrupted by another thread. In terms of implementation, CountDownLatch directly delegates the request to the AbstractQueuedSynchronizer#acquireSharedInterruptibly method of AQS for processing, so let's take a look at CountDownLatch's implementation of the template method AbstractQueuedSynchronizer#tryAcquireShared, as follows:
Protected int tryAcquireShared (int acquires) {return (getState () = = 0)? 1:-1;}
The implementation is very simple. Get and determine whether the status value is 0. If so, the current thread successfully acquires resources and can continue to execute. Otherwise, it means that the current thread failed to obtain resources and needs to be added to the synchronization queue to block waiting. CountDownLatch also defines a timeout version of CountDownLatch#await (long, TimeUnit) for the CountDownLatch#await method.
Let's move on to the CountDownLatch#countDown method, which subtracts the count value by 1 and wakes up the waiting thread if the count value is found to be 0. In terms of implementation, CountDownLatch also directly delegates the request to the AbstractQueuedSynchronizer#releaseShared method of AQS for processing, so let's take a look at CountDownLatch's implementation of the template method AbstractQueuedSynchronizer#tryReleaseShared, as follows:
Protected boolean tryReleaseShared (int releases) {/ / Decrement count; signal when transition to zero for (;;) {/ / get the state status value int c = getState (); / / if 0 means there are no resources to release, return false directly to prevent state from becoming negative if (c = 0) {return false } / / the number of resources minus 1 int nextc = c-1; if (compareAndSetState (c, nextc)) {/ / if 0 means that all resources have been released, and the waiting thread needs to be awakened return nextc = = 0;}
Specific implementation, such as code comments, is relatively simple. It is important to note that the zero operation in the above step may feel a little redundant at first, but the main purpose of this step is to ensure that the state value does not become negative. When the current count value changes to 0, the above method returns true, and then AQS wakes up threads that were previously queued for a count value other than 0.
At this point, the study of "how to realize the CountDownLatch of JUC" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.