package easy;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Solution66 {
//初始化线程数
public static final int NUMBER = 3;
public static void main(String[] args) {
//初始化线程池
ThreadPoolExecutor executor = new ThreadPoolExecutor(NUMBER, NUMBER, 300,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.CallerRunsPolicy());
System.out.println("---技能描述:@千里奔袭(突刺一段随机距离)");
System.out.println("---马匹准备就绪,比赛开始");
//从池中生成三个线程
for (int i = 0; i < NUMBER; i++) {
executor.execute(new StartGame());
}
executor.shutdown();
}
}
package easy;
public class StartGame extends Thread {
//初始化终点和两匹马的当前路程
static int destination = 30;
static int horseDes1 = 0;
static int horseDes2 = 0;
//记录技能的使用情况
static boolean horse1UseSkill = true;
static boolean horse2UseSkill = true;
String horse1UseSkillMsg = "";
String horse2UseSkillMsg = "";
//记录弹射起步的状态
static boolean status = true;
//记录播报比赛结果的状态
static boolean matchStatus = true;
public static final Object WAIT_MONITOR = new Object();
@Override
public void run() {
StartGame game = new StartGame();
synchronized(WAIT_MONITOR) {
if (status) {
game.jump();
}
}
try{
game.listenHalf();
game.match();
Thread.sleep(500);
synchronized(WAIT_MONITOR) {
if (matchStatus) {
game.showResult();
}
}
}catch (Exception e){
}
}
//弹射起步福利
public void jump(){
if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-1马")) {
System.out.println(Thread.currentThread().getName() + "马率先弹射起步,前进了5m");
horseDes1 = horseDes1 + (int)(Math.random()*10);
status = false;
}else if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-2马")) {
System.out.println(Thread.currentThread().getName() + "马率先弹射起步,前进了5m");
horseDes2 = horseDes2 + (int)(Math.random()*10);
status = false;
}
}
//每隔5s,播报比赛状态
public void match() throws InterruptedException {
while (horseDes1 < destination && horseDes2 < destination) {
Thread.sleep(5000);
if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-1马")) {
horseDes1 = horseDes1 + (int)(Math.random()*10);
//技能使用情况
if ((int)(Math.random()*10) % 2 == 0 && horse1UseSkill) {
horseDes1 = horseDes1 + (int)(Math.random()*10);
horse1UseSkillMsg = ",并在此次行动使用了@千里奔袭技能";
horse1UseSkill = false;
}
System.out.println(Thread.currentThread().getName() + "马目前前进了" + horseDes1 +"米" + horse1UseSkillMsg);
horse1UseSkillMsg = "";
}else if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-2马")) {
horseDes2 = horseDes2 + (int)(Math.random()*10);
//技能使用情况
if ((int)(Math.random()*10) % 2 == 0 && horse2UseSkill) {
horseDes2 = horseDes2 + (int)(Math.random()*10);
horse2UseSkillMsg = ",并在此次行动使用了@千里奔袭技能";
horse2UseSkill = false;
}
System.out.println(Thread.currentThread().getName() + "马目前前进了" + horseDes2 +"米" + horse2UseSkillMsg);
horse2UseSkillMsg = "";
}
}
}
//播报比赛结果
public void showResult() {
if (horseDes1 >= destination && (Thread.currentThread().getName() + "马").equals("pool-1-thread-1马")) {
System.out.println(Thread.currentThread().getName() + "马赢得了比赛,恭喜它!");
matchStatus = false;
}else if (horseDes2 >= destination && (Thread.currentThread().getName() + "马").equals("pool-1-thread-2马")) {
System.out.println(Thread.currentThread().getName() + "赢得了比赛,恭喜它!");
matchStatus = false;
}
}
//新起一个线程,用来监听赛程过半消息
public void listenHalf() {
if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-3马")) {
while (true) {
if (horseDes1 >= destination / 2) {
System.out.println("---赛程已过半,比赛进入白热化阶段");
break;
}else if (horseDes2 >= destination / 2) {
System.out.println("---赛程已过半,比赛进入白热化阶段");
break;
}
}
}
}
}
效果图如下所示: