模拟淘宝集齐5种福卡,坐等开奖案例
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/** * @description: 模拟淘宝集齐5种福卡,坐等开奖案例 * @author: xz */
public class CyclicBarrierDemo {
public static void main(String[] args) {
collectCard();
}
private static void collectCard(){
CyclicBarrier cyclicBarrier=new CyclicBarrier(5,()->{
System.out.println("五种卡片已集齐,坐等开奖");
});
for (int i = 1; i <=5; i++) {
final int temp = i;
new Thread(()->{
System.out.println("线程"+Thread.currentThread().getName()+"\t 收集到第"+ temp +"种福卡");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://wwwxz.blog.csdn.net/article/details/122353390
内容来源于网络,如有侵权,请联系作者删除!