Copy Code code as follows:
Package Com.yao;
Import Java.util.concurrent.CountDownLatch;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
/**
* Countdownlatch is a counter, it has an initial number,
* The thread waiting for this counter must wait until the counter countdown to 0 o'clock to continue.
*/
public class Countdownlatchtest {
/**
* Initialize the thread of the component
*/
public static class Componentthread implements Runnable {
Counter
Countdownlatch latch;
Component ID
int ID;
Construction method
Public Componentthread (countdownlatch latch, int ID) {
This.latch = latch;
This.id = ID;
}
public void Run () {
//initialize the component
system.out.println (" Initializing component "+ ID);
try {
thread.sleep (+ ID);
  &NBSP} catch (Interruptedexception e) {
}
System.out.println ("Component" + ID + "initialized!");
//The counter by one
latch.countdown ();
 &NBSP}
}
/**
* Start the server
*/
public static void StartServer () throws Exception {
System.out.println ("Server is starting.");
Initializes a countdownlatch with an initial value of 3
Countdownlatch latch = new Countdownlatch (3);
3 Threads to start 3 components separately
Executorservice service = Executors.newcachedthreadpool ();
Service.submit (new Componentthread (latch, 1));
Service.submit (new Componentthread (latch, 2));
Service.submit (new Componentthread (latch, 3));
Service.shutdown ();
Waiting for initialization of 3 components is complete
Latch.await ();
When the required three components are complete, the server can continue
System.out.println ("Server is up!");
}
public static void Main (string[] args) throws Exception {
Countdownlatchtest.startserver ();
}
}