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); Constructs a method parameter, specifying the count number of times
public void Countdown(); When this method is called, the count is reduced by one
public void await(); Calling this method will block the current thread until the value of the timer is 0
Code:
Import Java.util.concurrent.countdownlatch;public class Countdownlatchtest {public static void main (string[] args) Throws Interruptedexception {Countdownlatch latch = new Countdownlatch (3); new worker (1, latch). Run (); new Worker (2, latch ). Run (); New Worker (3, latch). Run (); latch.await (); System.out.println ("Work-down ...");}} Class Worker extends thread{private int i;private countdownlatch latch;public Worker (int i, countdownlatch latch) {this.i = I;this.latch = latch;} @Overridepublic void Run () {if (i = = 3) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();}} Latch.countdown (); System.out.println ("worker" + i + "done ...");}}
This article is from the architect's path blog, so be sure to keep this source http://lizhuquan0769.blog.51cto.com/2591147/1785491
"Java Multithreading" Countdownlatch Synchronization helper Class