龟兔比赛 多线程

/***

  • 龟兔比赛
    *编写龟兔赛跑多线程程序,设置赛跑长度为30米
  • 兔子的速度是10米每秒 兔子跑完10米后休眠的时间为10秒
  • 乌龟的速度是1米每秒 乌龟跑完10米的休眠时间是1秒
  • 要求兔子和乌龟的线程西结束 主线程才能公布最后的结果

*/

public class TestWuGuiTrace {
	
	
	public static void main(String[] args) {
		WuGui w = new WuGui() ;
		w.start(); 
		Rabbit rabbit =  new Rabbit() ; 
		rabbit.start();
		try {
			rabbit.join();
			w.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long wT = w.getTime();
		long rT = rabbit.getTime() ;
		if(wT>rT) {
			System.out.println("兔子赢 ");
		}else if(wT==rT) {
			
			System.out.println("平手 ");
		}else {
			System.out.println("乌龟赢 ");
		}
		
		
	}
}
class WuGui extends Thread{
		private long time  ;
		
		
	public long getTime() {
			return time;
		}

	public void run () {
		long start = System.currentTimeMillis() ;
		System.out.println("乌龟run ......");
		for(int i = 0;i<=30 ;i++) {
			try {
				System.out.println("乌龟跑了"+i+"米");
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(i==10 || i==20 ) {
				try {
					System.out.println("乌龟已经跑了"+i+"米");
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		long end  = System.currentTimeMillis() ; 
		time = end-start ;
		System.out.println("乌龟到达终点 ");
	}
	
}
class Rabbit extends Thread{
	private long time  ;
	public long getTime() {
		return time;
	}
	public void run () {
		long start = System.currentTimeMillis() ;
		System.out.println("兔子run 。。。");
		for(int i = 0 ;i<=30;i++ ) {
			System.out.println("兔子跑了"+i+"米");
			try {
				
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(i==10 || i== 20 ) {
				try {
					System.out.println("兔子已经跑了"+i+"米");
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		long end = System.currentTimeMillis() ;
		time = end-start ;
		System.out.println("兔子已经到达终点");
	}
}

使用面向对象编写龟兔赛跑


public class TestSport {
	public static void main(String[] args) {
		Sport s1 = new Sport("乌龟",1000,1000);
		Sport s2 = new Sport("兔子",100,10000);
		
		s1.start();
		s2.start();
		
		//主线程被加塞
		try {
			s1.join();
			s2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		long wT = s1.getTime();
		long tT = s2.getTime();
		if(wT < tT){
			System.out.println("乌龟赢");
		}else if(wT > tT){
			System.out.println("兔子赢");
		}else{
			System.out.println("平手,再来一次");
		}
		
	}
}


class Sport extends Thread{
	

	private long speedPerMeter ;
	private long restTime ;
	private long time;
	
	public Sport(String name ,long speedPerMeter, long restTime) {
		super(name);
		this.speedPerMeter = speedPerMeter;
		this.restTime = restTime;
	}
	


	public void run(){
		long start = System.currentTimeMillis();
		for (int i = 1; i <= 30; i++) {
			System.out.println(getName() + "run....");
			try {
				Thread.sleep(speedPerMeter);//一秒,模拟跑一米耗时1秒
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(i==10 || i==20){
				System.out.println(getName()+"跑了" + i + "米");
				System.out.println(getName()+"sleep...");
				try {
					Thread.sleep(restTime);//1秒,模拟休息1秒
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		long end = System.currentTimeMillis();
		time = end - start;
		System.out.println(getName()+"到达终点");
	}

	public long getTime() {
		return time;
	}
	
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值