CountDownLatchWating

The Redefine Team Lv5
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchWating {

    private final static Logger logger = LoggerFactory.getLogger(CountDownLatchWating.class);

    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();

        final CountDownLatch cdl = new CountDownLatch(3);

        service.execute(()-> {
            try {
                logger.info("working...");

                cdl.await();

            } catch (InterruptedException e) {
                e.printStackTrace();
                logger.error("Exception = {}", e);
            }
        });

        for (int i = 0; i < 3; i++) {
            final int tid = i;
            service.execute(() -> {
                try {
                    Thread.sleep((long) (Math.random() * 5000));

                    cdl.countDown();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        service.shutdown();
    }

}
  • 标题: CountDownLatchWating
  • 作者: The Redefine Team
  • 创建于 : 2016-06-20 06:26:50
  • 更新于 : 2023-05-23 18:52:03
  • 链接: https://redefine.ohevan.com/2016/06/20/countdownlatchwating/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
CountDownLatchWating