Java5 countdownlatch Sync Tool

Source: Internet
Author: User

Like a countdown counter, the countdown method that calls the Countdownlatch object will subtract 1 from the counter, and when it arrives 0 o'clock, all the waiting will start executing.

Java.util.concurrent.CountDownLatch
A synchronous helper class that allows one or more threads to wait until a set of operations that are performed in another thread is completed. Initializes the countdownlatch with the given count. Because the countdown () method is called, the await method is blocked until the current count reaches 0. After that, all the waiting threads are freed, and all subsequent calls to await are returned immediately. This behavior occurs only once-the count cannot be reset. If you need to reset the count, consider using Cyclicbarrier.

Countdownlatch is a universal synchronization tool that has many uses. Countdownlatch, which counts 1 initialized, is used as a simple on/Guan, or Portal: All threads that call await are waiting at the entrance until a thread that calls Countdown () opens the portal. N-initialized Countdownlatch can cause a thread to wait until N threads complete an operation, or to wait until an operation completes N.

A useful feature of Countdownlatch is that it does not require a thread that calls the countdown method to wait until the count arrives 0 o'clock, and before all threads can pass, it simply prevents any thread from continuing through an await.

Example: Multiple athletes waiting for the referee to order: The referee and all the athletes to post the results

Package Com.ljq.test.thread;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class CountdownLatchTest {public static void Main (string[] args) {Executorservice service = Executors.newcachedthreadpool ();//The referee publishes the counter of the command, the counter is 0, The athlete runs final countdownlatch cdorder = new Countdownlatch (1);//The player runs to the end of the counter, announcing the result for 0 Referee final Countdownlatch cdanswer = new Countdownlatch (3);//Generate 3 players for (int i=0;i<3;i++) {Runnable Runnable = new Runnable () {public void run () {try { SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "ready to accept Command"); cdorder.await (); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "accepted command"); Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "response command processing result"); Cdanswer.countdown (); catch (Exception e) {e.printstacktrace ();}}}; Service.execute (runnable); The athlete starts the task}try {//The referee will take a break and then release the Command Thread.Sleep ((long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.)CurrentThread (). GetName () + "forthcoming command"); Cdorder.countdown (); The command counter is set to 0, and the command System.out.println ("thread" + thread.currentthread (). GetName () + "sent command, waiting for results"); cdanswer.await (); Wait for all players, counter for 0 all athletes in place System.out.println ("thread" + thread.currentthread (). GetName () + "All responses received");} catch (Exception e) {e.printstacktrace ();} Service.shutdown ();}}

return Result:

Thread pool-1-thread-3 is preparing to accept the command thread pool-1-thread-1 is preparing to accept the command thread pool-1-thread-2 is preparing to accept the command thread, main is about to publish the command thread, main has sent the command, Waiting for result thread pool-1-thread-2 accepted command thread pool-1-thread-1 accepted command thread pool-1-thread-3 accepted command thread pool-1-thread-3 response command processing result thread pool-1-thread-2 response command processing knot Fruit Thread pool-1-thread-1 Response command processing result thread main has received all response results

  

Java5 countdownlatch Sync Tool

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.