PackageCom.thread.test.Lock;ImportJava.util.Random;ImportJava.util.concurrent.CountDownLatch;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classCountdownlatchdemoImplementsRunnable {Static FinalCountdownlatch end =NewCountdownlatch (10);//Create a countdown timer to indicate that you need to wait for 10 threads to wait for a thread on Countdownlatch to continue execution Static FinalCountdownlatchdemo DEMO =NewCountdownlatchdemo (); Public voidrun () {Try{Thread.Sleep (NewRandom (). Nextint (10) * 1000); System.out.println (Thread.CurrentThread (). GetName ()+ "----Check complete"); End.countdown ();//notifies the countdown timer that a thread has finished, and the countdown timer minus 1.}Catch(interruptedexception e) {e.printstacktrace (); } } Public Static voidMain (string[] args)throwsinterruptedexception {//use a countdown timer to mimic a rocket launch.Executorservice exec = Executors.newfixedthreadpool (10);//Create a thread pool of 10 threads for(inti = 0; I < 10; i++) {exec.submit (DEMO); } end.await ();//when the countdown timer waits for 10 threads to complete, the main thread can continue to executeSYSTEM.OUT.PRINTLN ("Rocket launch"); Exec.shutdown ();//Close the thread pool }}
Multi-line programmable tool class-Countdown timer Countdownlatch use (mimic rocket launch)