Multi-line programmable tool class-Countdown timer Countdownlatch use (mimic rocket launch)

Source: Internet
Author: User

 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)

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.