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.
Main methods
public Countdownlatch (int count);
public void Countdown();
public void await() throws Interruptedexception
The constructor method parameter specifies the number of times the count
Countdown method, the current thread calls this method, then the count is reduced by one
Awaint method, calling this method will block the current thread until the value of the timer is 0
Example
Java code
- Public class Countdownlatchdemo {
- final static SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- public static void Main (string[] args) throws interruptedexception {
- Countdownlatch latch=New Countdownlatch (2); Collaboration between two workers
- Worker worker1=new worker ("Zhang San", latch);
- Worker worker2=new Worker ("Li si", 8000, latch);
- Worker1.start (); //
- Worker2.start (); //
- Latch.await (); //Waiting for all workers to finish their 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;
- }
- public Void Run () {
- System.out.println ("Worker" +workername+ "do-work-begin at" +sdf.format (New Date ()));
- DoWork (); //Work out
- System.out.println ("Worker" +workername+ "do Work Complete at" +sdf.format (New Date ()));
- Latch.countdown (); //Workers finish work, counter minus one
- }
- private void DoWork () {
- try {
- Thread.Sleep (Worktime);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- }
- }
- }
public class Countdownlatchdemo {final static SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); public static void Main (string[] args) throws interruptedexception {Countdownlatch latch=new countdownlatch (2);//Two workers ' Collaborative worker worker1=new worker ("Zhang San", latch); Worker worker2=new worker ("Li Si", 8000, latch); Worker1.start ();//Worker2.start ();//latch.await ();//wait for all workers to complete the work SYSTEM.OUT.PRINTLN Ormat (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; public void Run () {System.out.println ("Worker" +workername+ "does work begin at" +sdf.format (new Date ())); DoWork ();///Worked System.out.println ("Worker" +workername+ "do job complete at" +sdf.format (new Date ())); Latch.counTdown ();//worker completes work, counter minus one} private void DoWork () {try {thread.sleep (worktime);} catch (Interruptedexception e) {E.printstacktrace ();} } } }
Output:
Worker Zhang San do work begin at 2011-04-14 11:05:11 worker Li Si does work begin at 2011-04-14 11:05:11 worker Zhang San D o Work complete at 2011-04-14 11:05:16 Worker Li Si does work in 2011-04-14 11:05:19 all work done at 2011-04-14 11 : 05:19
Countdownlatch use of Java