Countdownlatch countdown timer effect, thread stops at await, when Countdown
It's 0 o'clock in the passage.
Under the simulated life order, three threads start executing, and then after the three threads are executed, then the next stage of operation
Package Com.condition;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.ExecutorService; Import Java.util.concurrent.executors;public class Countdownlatchtest {public static void main (string[] args) { Executorservice ThreadPool = Executors.newcachedthreadpool (); final Countdownlatch cdorder = new Countdownlatch (1); Final Countdownlatch Cdcountdown = new Countdownlatch (3); for (int i = 0;i < 3;i++) {Runnable Runnable = new Runnable () { @Overridepublic void Run () {System.out.println (Thread.CurrentThread (). GetName () + "ready to accept command"); try {cdorder.await ();} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "executing command"); try {thread.sleep ((long) (Math.random () *1000)),} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "responding"); Cdcountdown.countdown (); }}; Threadpool.execute (runnable);} try {thread.sleep ((long) (Math.random () *1000));} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Synchronized (ThreadPool) {System.out.println (Thread.CurrentThread (). GetName () + "forthcoming command"); Cdorder.countdown (); System.out.println (Thread.CurrentThread (). GetName () + "issuing command"); try {cdcountdown.await ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "all results received");}}
Multi-threaded Learning 7--countdownlatch