Java thread assigns component Countdownlatch use in class library

Source: Internet
Author: User
Tags lenovo

Package Org.rui.thread.newc;import Java.util.random;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.executor;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Import java.util.concurrent.timeunit;/** * Latch latch * Widget in new class library Countdownlatch * * @author Lenovo * */class taskportion Implements Runnable {private static int counter = 0;private final int id = counter++;p rivate static Random rand = new Rand Om (rivate);p final countdownlatch latch;public taskportion (countdownlatch latch) {this.latch = latch;} @Overridepublic void Run () {try {doWork (); Latch.countdown ();} catch (Interruptedexception e) {//acceptable to exit}}/ /process business code public void DoWork () throws Interruptedexception {TimeUnit.MILLISECONDS.sleep (Rand.nextint (2000)); System.out.println (this + "done");} Public String toString () {return String.Format ("%1$-3d", id);}} Waits on the Countdownlatchclass Waitingtask implements Runnable {private static int counter = 0;//count PrivateFinal int id = counter++;p rivate static random rand = new random;p rivate final countdownlatch latch; Waitingtask (Countdownlatch latch) {this.latch = latch;} The task of @Overridepublic Void Run () {try {///Call Countdown () is not blocked when the call is generated, only the call to await is blocked until the count reaches the 0//wait for the problem resolved task to invoke await on this latch (), they are stopped by themselves until the latch count ends latch.await (); SYSTEM.OUT.PRINTLN ("Latch barrier is considered" + this);} catch (Interruptedexception e) {System.out.println (this + "interrupted");}} Public String toString () {return String.Format ("Waitingtask%1$-3d", id);}} /** * Taskportio will sleep randomly for a period of time to simulate the completion of this part of the work, while Waitingtask represents the waiting part of the system, which waits until the initial part of the problem is finished, * All tasks are used to define the same single COUNDOWNLACTH in main * * @author Lenovo * */public class Countdownlatchdemo {static final int SIZE = 100 ;p ublic static void Main (string[] args) throws interruptedexception {Executorservice exec = Executors.newcachedthreadpool (); Countdownlatch latch = new Countdownlatch (SIZE);//must share a Countdownlatch object for (int i = 0; i < i++) {Exec.execute (NE W waitingtask (latch));//This waits for the latch above to be executed 0 o'clock.}for (int i = 0; i < SIZE; i++) {Exec.execute (new taskportion (latch));} Latch.await ();  SYSTEM.OUT.PRINTLN ("launched All Tasks"); Exec.shutdown ();//Quit when all task complete}}/**output:launched all TASKS43 Complete 95 finish 99 finish 36 finish 94 finish 11 finish .... 12 Completion 1 Finish 27 finish 98 finish 13 finish 72 finish 71 finish 2 finish 45 finish 92 finish 31 finish 14 finish 17 finish 6 Finish 97 complete ... 80 complete .... 56 Completion 85 Completion 61 Completion 30 complete .... 3 Completion 93 Completion 81 Completion 78 Completion 73 Completion 44 Completion 82 Completion 49 Completion 64 Completion 83 Complete 16 complete latch barrier is considered Waitingtask 2 latch barrier is considered waitingtask 0 LA TCH Barrier is considered to be Waitingtask 4 latch barrier is considered Waitingtask 1 latch barrier is considered waitingtask 5 latch barrier is considered waitingtask 3 latch barrier is considered wait Ingtask 7 latch Barrier is considered waitingtask 6 latch barrier is considered Waitingtask 9 latch barrier is considered waitingtask 8 */


Java thread assigns component Countdownlatch use in class library

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.