package ThreadDemo;
// 龟兔赛跑,总路程共一百步
public class Test07_Race implements Runnable{
boolean flag;
String winner;
@Override
public void run() {
for (int i = 1; i <=100; i++) {
flag=isGameOver(i);
if (flag==true){
break;
} // 有winner之后,游戏结束
System.out.println(Thread.currentThread().getName()+"-->跑了 "+i+"步");
}
}
public boolean isGameOver(int step){
if (winner!=null){
return true;
}
if (step>=100){
winner=Thread.currentThread().getName();
}
return false;
}
public static void main(String[] args) {
Test07_Race race = new Test07_Race();
new Thread(race,"乌龟").start();
new Thread(race,"兔子").start();
}
}
标签:赛跑,龟兔,07,Thread,winner,public,new,Test07
From: https://www.cnblogs.com/zhanjianhai/p/17664969.html