countdownlatch(java CountDownLatch)

这个工具用于控制多线程(点位)执行情况,当子线程执行完逻辑则执行:countDownLatch.countDown(); 当监测点位都执行完业务逻辑后:countDownLatch.await(); 放行,否则阻塞主线程。/** * @a...

这个工具用于控制多线程(点位)执行情况,当子线程执行完逻辑则执行:

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"); }}

  • 发表于 2022-11-26 14:42:14
  • 阅读 ( 129 )
  • 分类:科技

0 条评论

请先 登录 后评论
王春群
王春群

461 篇文章

你可能感兴趣的文章

相关问题