public class ArmorWarrior {
//5个铠甲召唤人 private static final int num = 5;
//召唤器启动 private static final CyclicBarrier cyclicBarrier = new CyclicBarrier(
num, () -> { System.out.println("等待所有铠甲召唤人,完成合体...");
System.out.println("光影铠甲,出击!");
});
private static final ExecutorService executorService = Executors.newFixedThreadPool(num);
public static void main(String[] args) throws Exception {
executorService.submit(() -> {
//炎龙侠核心成员,全面
System.out.println("炎龙侠:炎龙铠甲,合体!");
try {
Thread.sleep(3000);
System.out.println("炎龙铠甲完成合体!");
cyclicBarrier.await();
System.out.println("炎龙侠出击!");
} catch (Exception e) {
e.printStackTrace();
} });
executorService.submit(() -> {
//风鹰侠速度快,合体也快
System.out.println("风鹰侠:风鹰铠甲,合体!");
try {
Thread.sleep(2000);
System.out.println("风鹰铠甲完成合体!");
cyclicBarrier.await(); System.out.println("风鹰侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.submit(() -> {
//黑犀侠最磨叽,合体最慢
System.out.println("黑犀侠:黑犀铠甲,合体!");
try {
Thread.sleep(4000);
System.out.println("黑犀铠甲完成合体!");
cyclicBarrier.await();
System.out.println("黑犀侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.submit(() -> {
//雪獒侠,力量最强,直接用金晶石能量直接合体
System.out.println("雪獒侠:(还需要召唤器?!)...!");
try {
Thread.sleep(1000);
System.out.println("雪獒铠甲完成合体!");
cyclicBarrier.await();
System.out.println("雪獒侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.submit(() -> {
//黑犀侠最磨叽,合体最慢
System.out.println("地虎侠:地虎铠甲,合体!");
try {
Thread.sleep(3500);
System.out.println("地虎铠甲完成合体!");
cyclicBarrier.await();
System.out.println("地虎侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.shutdown();
}
}
多线程小程序——铠甲勇士(CyclicBarrier)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...