1.private static CountDownLatch cdl = new CountDownLatch(200); //設置併發數
2.
for(int i = 0;i<200; i++){
new Thread(new Ticket()).start(); //創建線程
cdl.countDown(); //一直等待計數器到0,然後200線程同時啓動
}
//讓主進程休眠3秒,等所有線程都跑完才結束
Thread.currentThread().sleep(3000);
3.
public Class Ticket Implements Runnable{
@Override
public void run (){
cdl.await(); //之前所有的線程處於等待狀態
System.out.println("買票");
}
}