Java Concurrency-An analysis of the usage of Countdownlatch

Source: Internet
Author: User

Countdownlatch is 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 calling 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.

Simple example

 Packagecom.huey.hello.concurrency;ImportJava.util.concurrent.CountDownLatch;/** *  * @authorHuey *@version1.0 * @created 2015-2-28*/ Public classWorkerImplementsRunnable {PrivateString Workername; Final PrivateCountdownlatch donesignal;  PublicWorker (String workername, Countdownlatch donesignal) { This. Workername =Workername;  This. donesignal =donesignal; } @Override Public voidrun () {Try {             This. Work (); System.out.println (Workername+ "has completed the job.");        Donesignal.countdown (); } Catch(Exception e) {}}Private voidWork ()throwsinterruptedexception {System.out.println (workername+ "is working."); Thread.Sleep (3000); }}

Test

 Packagecom.huey.hello.concurrency;ImportJava.util.concurrent.CountDownLatch;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;/** *  * @authorHuey *@version1.0 * @created 2015-2-28*/ Public classDriver {Static Final intN = 3;  Public Static voidMain (string[] args)throwsException {countdownlatch donesignal=NewCountdownlatch (N); Executorservice Pool=Executors.newfixedthreadpool (N);  for(inti = 1; I <= N; i++) {String workername= "NO." +i; Pool.execute (NewWorker (Workername, donesignal));                } pool.shutdown (); System.out.println ("Waiting for all workers to complete their jobs.");                Donesignal.await (); System.out.println ("All workers have completed their jobs."); System.out.println ("Continue ..."); }    }

Test output

The working is. No.2 is working. Waiting for all workers to complete their jobs. No.3 is working. The completed has the job. No.2 has completed the job. No.3 has completed the job. All workers have completed their jobs. Continue ...

Java Concurrency-An analysis of the usage of Countdownlatch

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.