这个工具用于控制多线程(点位)执行情况,当子线程执行完逻辑则执行:
countDownLatch.countDown();
当监测点位都执行完业务逻辑后:
countDownLatch.await();
放行,否则阻塞主线程。
/** * @author: leo * @date: 2022/10/12 */public class client { private static final CountDownLatch countDownLatch = new CountDownLatch(2); public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread()+"1"); countDownLatch.countDown(); } }).start(); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread() + "2"); countDownLatch.countDown(); } }).start(); countDownLatch.await(); System.out.println(Thread.currentThread()+"main thread"); }}