public class ArmorWarrior {
//5个铠甲召唤人
private static final int num =5;
private static final int initNum =0;
//召唤器
private static final Semaphore semaphore =new Semaphore(initNum);
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("炎龙铠甲完成合体!");
semaphore.release();
System.out.println("炎龙侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.submit(() ->{
//风鹰侠速度快,合体也快
System.out.println("风鹰侠:风鹰铠甲,合体!");
try {
Thread.sleep(2000);
System.out.println("风鹰铠甲完成合体!");
semaphore.release();
System.out.println("风鹰侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.submit(() ->{
//黑犀侠最磨叽,合体最慢
System.out.println("黑犀侠:黑犀铠甲,合体!");
try {
Thread.sleep(4000);
System.out.println("黑犀铠甲完成合体!");
semaphore.release();
System.out.println("黑犀侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.submit(() ->{
//雪獒侠,力量最强,直接用金晶石能量直接合体
System.out.println("雪獒侠:(还需要召唤器?!)...!");
try {
Thread.sleep(1000);
System.out.println("雪獒铠甲完成合体!");
semaphore.release();
System.out.println("雪獒侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.submit(() ->{
//黑犀侠最磨叽,合体最慢
System.out.println("地虎侠:地虎铠甲,合体!");
try {
Thread.sleep(3500);
System.out.println("地虎铠甲完成合体!");
semaphore.release();
System.out.println("地虎侠出击!");
} catch (Exception e) {
e.printStackTrace();
}
});
System.out.println("等待所有铠甲召唤人,完成合体...");
semaphore.acquire(num);
System.out.println("光影铠甲,出击!");
executorService.shutdown();
}
}
网友评论