欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

龟兔赛跑

程序员文章站 2022-06-10 20:52:49
...
public class race implements Runnable(){
	//胜利者
	private static String winner;

	//模拟兔子睡觉
	if(Thread.currentThread().getname().equal("兔子") && i%10==0){
		Thread.sleep(100);
	}
	
	public void run(){
		for(int i = 0; i<=100,i++){
			//判断比赛是否结束
			boolean flag = gameover(i);
			if(flag){
				break;
				}
		
			System.out.println(Thread.currentThread.getname()+"--跑了"+i+"步");
		}
	}
	
	//判断是否完成比赛
	private boolean gameover(int step){
		//判断是否有胜利者
		if(winner!=null){
			return ture;
		}{
			if(step>=100){
				winner = Thread.currentThread.getname();
				System.out.println("winner is"+ winner);
				return ture;
				}
			}
			return false;
	}


public static void main(String[] args){
		Race race = new Race();
		
		new Thread(race,name:"兔子").start();
		new Thread(race,name:"乌龟").start();
			}

}