龟兔赛跑
package kuang.demo;
public class Race implements Runnable{
private static String winner;
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
if(Thread.currentThread().getName().equals("泰罗")&&i%10==0){
try{
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
boolean flags = gameover(i);
if(flags){
break;
}
System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"米");
}
}
private boolean gameover(int step){
if(winner != null){
return true;
}{if(step>=100){
winner = Thread.currentThread().getName();
System.out.println("winner is"+winner);
return true;
}
}
return false;
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"泰罗").start();
new Thread(race,"雷欧").start();
}
}