• CountDownLatch测试


    /**
    * 调用方法 countDown(),会将初始化的值减少一次;
    * 调用方法 await(),线程会在此等待,直到计时器为0,即被唤醒
    */

    public static void main(String[] args) { final ThreadPoolExecutor countDownLatchExecutor = new ThreadPoolExecutor( 1, 10, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder() .setNameFormat("CountDownLatch-Executor-%d") .setDaemon(true) .build() ); CountDownLatch latch = new CountDownLatch(T_COUNT); for (int i = 0; i < T_COUNT; i++) { Runnable task = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "执行完毕,等待其他线程执行"); latch.countDown(); } }; countDownLatchExecutor.submit(task); } try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " 任务结束,主线程执行"); countDownLatchExecutor.shutdown(); }
  • 相关阅读:
    git 常用命令
    centos 7 mini 安装
    python打印杨辉三角
    python 求100内的素数/质数
    字符串与bytes
    format
    Python字符串格式化
    数据结构
    ARM工作模式
    C语言实现字符串逆序输出
  • 原文地址:https://www.cnblogs.com/mmh760/p/14308165.html
Copyright © 2020-2023  润新知