龜兔比賽 多執行緒

歷精圖治發表於2018-11-22

/***

  • 龜兔比賽
    *編寫龜兔賽跑多執行緒程式,設定賽跑長度為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;
	}
	
}

相關文章