package com.bjsxt.height.concurrent019; import java.util.concurrent.CountDownLatch; /** * The CountDownLatch class is located in the java.util.concurrent package, and it can be used to implement counter-like functions. * For example, there is a task A, * It can only be executed after the other 4 tasks are executed. At this time, CountDownLatch can be used to achieve this function * */ public class UseCountDownLatch { public static void main(String[] args) { final CountDownLatch countDown = new CountDownLatch(2);//The parameter indicates several countDown(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { System.out.println("Enter thread t1" + "Waiting for other threads to finish processing..."); countDown.await();//Block here, wait for other threads to finish executing before they can go down System.out.println("t1 thread continues to execute..."); } catch (InterruptedException e) { e.printStackTrace (); } } },"t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { try { System.out.println("t2 thread initializes..."); Thread.sleep(3000); System.out.println("The t2 thread is initialized, notify the t1 thread to continue..."); countDown.countDown();//After execution, notify } catch (InterruptedException e) { e.printStackTrace (); } } }); Thread t3 = new Thread(new Runnable() { @Override public void run() { try { System.out.println("t3 thread initializes..."); Thread.sleep(4000); System.out.println("The t3 thread is initialized, notify the t1 thread to continue..."); countDown.countDown();//After execution, notify } catch (InterruptedException e) { e.printStackTrace (); } } }); t1.start(); t2.start(); t3.start(); } }