java多线程模拟龟兔赛跑
让乌龟和兔子在同一个赛道从1开始跑到100,看看谁更快. 1 public class Racer implements Runnable{ 2 private static String winner;//胜利者 3 4 @Override 5 public void run() { 6 //赛道 7 for (int step = 1; step <= 100; step++) { 8 /* if(Thread.currentThread().getName().equals("兔子")&&step%50==0){ 9 try { 10 Thread.sleep(200); 11 } catch (InterruptedException e) { 12 e.printStackTrace(); 13 } 14 }*/ 15 System.out.println(Thread.currentThread().getName()+"---->走了"+step+"步"); 16 17 boolean flag= GameOver(step); 18 if(flag){ 19 break; 20 } 21 22 } 23 } 24 25 private boolean GameOver(int step) { 26 if(winner!=null){ 27 return true;