The difference between Countdownlatch and Cyclicbarrier

Source: Internet
Author: User

"Countdownlatch, Cyclicbarrier and semaphore"
http://www.cnblogs.com/dolphin0520/p/3920397.html "Countdownlatch Sync Tool Class" http://www.importnew.com/15731. Htmlcountdownlatch is what Countdownlatch is introduced in java1.5, the Concurrency tool class that was introduced with it, and Cyclicbarrier, Semaphore, Concurrenthashmap and Blockingqueue, they all exist under the Java.util.concurrent package. Countdownlatch This class can make a thread waits for other threads to finish their work before executing。 For example, the main thread of the application wants to execute after all the framework services have been started by the threads that are responsible for starting the framework service. Countdownlatch is through a counterTo implement, the initial value of the counter is the number of threads. Each time a thread finishes its task, the value of the counter is reduced by 1. When the counter value reaches 0 o'clock, it means that all the threads have completed the task, and then the thread waiting on the latch can resume the execution of the task. How Countdownlatch works the constructors defined in the Countdownlatch.java class://constructs a countdownlatch initialized with the given Count.public The Count value (count) in the void Countdownlatch (int count) {...} constructor is actually the number of threads that need to wait for the lockout. This value can only be set once, and Countdownlatch does not provide any mechanism to reset the count value. The first interaction with Countdownlatch is the main thread waiting for other threads. The main thread must be called immediately after starting another thread countdownlatch.await ()Method. This way the main thread will block on this method until other threads have completed their respective tasks. Other n threads must refer to the latching object because they need to notify the Countdownlatch object that they have completed their respective tasks. This notification mechanism is through Countdownlatch.countdown ()method, each time this method is called, the count value initialized in the constructor is reduced by 1. So when n threads call this method, the value of Count equals 0, and the main thread can resume its own task by using the await () method. Usage scenarios in real-time systems 1. Achieve maximum parallelism: Sometimes we want to start multiple threads at the same time for maximum parallelism. For example, we want to test a singleton class. If we create a countdownlatch with an initial count of 1 and have all the threads waiting in this lock, we can easily complete the test. We just call the countdown () method once to get all the waiting threads to resume execution at the same time. 2. Wait for n threads to complete their tasks before starting execution: for example, the application startup class ensures that all n external systems are up and running before processing the user request. 3. Deadlock detection: A very handy scenario is that you can use n threads to access a shared resource, the number of threads in each test phase is different, and an attempt to generate a deadlock. Common face questions you can prepare the following Countdownlatch related questions for your next interview: Explain the Countdownlatch concept? What are the differences between Countdownlatch and cyclicbarrier? Give some examples of countdownlatch use? The main method in the Countdownlatch class? Countdownlatch (int) await () await (Long,timeunit) countdown () GetCount () toString Seeing a lot of people on the Internet is a simple understanding of the difference between Countdownlatch and Cyclicbarrier Countdownlatch is a one-time, and cyclicbarrier can continue to use after calling reset。 Well, if it's just that simple, I think Cyclicbarrier is simply named Resetablecountdownlatch, which is obviously not. My understanding is to look at these two classes from their design goals. The description inside the Javadoc is like this. COUNTDOWNLATCH:A synchronization aid that allows one or more threads to wait until A set of operations being performed in Other threads completes. CYCLICBARRIER:A synchronization aid that allows a set of threads to all wait for each other to reach A common barrier PO Int. Countdownlatch and Cyclicbarrier are essentially the same, with each thread setting Countdownlatch or Cyclicbarrier barrier point points, each thread counts into the condition queue, When executing to the barrier point, the callback method for Countdownlatch or cyclicbarrier is invoked, and the queue traversal in the condition whether all the thread has been executed to the barrier point points. However, the thread is not released, and the thread is consistent in the running state (which can be understood as waiting for all other thread to complete the barrier point). The difference is that each thread arrives at the barrier point of the countdownlatch points can call Countdown () Count-1, to all count 0, all other thread passes, continues to execute or ends. All of the barrier points that arrive at point Cyclicbarrier, the barrier point ends and continues execution. If it is not possible to reach all (interrupts, failures, or timeouts, etc.), setting brokenbarrierexception causes all "Thread" errors that call the await method, all or None (All-or-none) mode of destruction. Reset is also thrown brokenbarrierexception. Nutshell Cyclicbarrier for consistency, Countdownlatch simple counting

The difference between Countdownlatch and Cyclicbarrier

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.