Java thread assigns component Countdownlatch use in class library

Source: Internet
Author: User
Tags lenovo

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbglhbmdydwkxotg4/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

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 + "finished");} 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 @Overridepublic void Run () {try {///Call Countdown () is not blocked when the call is generated. Only the call to await will be blocked until the count reaches the 0//waiting for the problem to be solved by calling await () on this latch, and they are stopped until the latch count ends latch.await (); SYSTEM.OUT.PRINTLN ("Latch barrier is felt" + 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 part of the system waiting. It 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 = 1 00;public 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 is waiting for the latch to be shipped 0 o'clock.Row}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 Finished 95 finished 99 finished 36 finished 94 finished 11 finished .... 12 finished 1 finished 27 98 finished 13 finished 72 71 finished 2 finished 45 finished 92 finished 31 finished 14 finished 17 finished 6 finished 97 finished .... 80 complete .... 56 finished 85 finished 61 finished 30 finished .... 3 finished 93 finished 81 78 finished 73 finished 44 finished 82 finished 49 finished 64 finished 83 finished 16 finished latch barrier was felt Waitingtask 2 latch barrier was felt Waitingtask 0 LA TCH Barrier is felt Waitingtask 4 latch barrier is felt Waitingtask 1 latch barrier is felt Waitingtask 5 latch barrier is felt Waitingtask 3 latch barrier is felt wait Ingtask 7 latch barrier is felt Waitingtask 6 latch barrier is felt Waitingtask 9 latch barrier is felt 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.