Java Countdownlatch Thread Helper classes

Source: Internet
Author: User

package aaa.bbb;import java.text.simpledateformat;import java.util.date;import  Java.util.concurrent.countdownlatch;public class t1 {final static simpledateformat  sdf = new simpledateformat ("Yyyy-mm-dd hh:mm:ss");p ublic static void  Main (String[] args)  throws exception {countdownlatch countdownlatch = new  countdownlatch (3); Worker w1 = new worker ("W1",  2000, countdownlatch); Worker w2 = new worker ("W2",  5000, countdownlatch); W1.start ();//w2.start ();// Countdownlatch.await ();//  waits for all workers to complete the work System.out.println ("all work done at "  +  sdf.format (New date ()));} static class worker extends thread {string workername;int worktime; Countdownlatch latch;public worker (string workername, int worktime,  Countdownlatch laTCH)  {this.workerName = workerName;this.workTime = workTime;this.latch =  latch;} @Overridepublic  void run ()  {system.out.println ("worker "  + workername +   " do work begin at " + sdf.format (New date ())); Try {thread.sleep ( Worktime);}  catch  (interruptedexception e)  {e.printstacktrace ();} System.out.println ("worker "  + workerName +  " do work complete at   "+ sdf.format (New date ())); Latch.countdown ();//  Worker completes work, counter minus one}}}


Java Countdownlatch Thread Helper classes

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.