countDownLatch代码练习
网站首页 文章专栏 countDownLatch代码练习
countDownLatch代码练习
编辑时间:2019-07-08 10:15 作者:毛毛小妖 浏览量:33 评论数:0
package com.demo.juc;

import java.util.concurrent.CountDownLatch;

import com.demo.enumTest.TestEnum;

public class TestCountDownLatch {
	public static void main(String[] args) {
		final CountDownLatch latch = new CountDownLatch(6);
		
		for(int i=1;i<=6;i++){
			final int index = i;
			new Thread(()->{
				System.out.println(Thread.currentThread().getName()+"国于公元"+TestEnum.get(index).getYear()+"年被灭。");
				latch.countDown();
			}, TestEnum.get(index).getName()).start();
		}
		
		
		//六国灭了之后,秦国才能一统天下
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.err.println("最终秦国一统天下");
	}
}

 

来说两句吧
最新评论
    还没有人评论哦,快来坐沙发吧!