package thread;
import java.util.Date;
/**
* 17.我们从小就知道“龟兔赛跑”的寓言故事,编写程序模拟实现。
提示:假定总路程为6000m,兔子每分钟跑300m,乌龟每分钟跑100m
,兔子每跑8分钟就休息(设定程序每执行1秒代表现实中的1分钟,
即兔子跑8分钟这个过程在计算机中用8s来完成),
也就是兔子每跑2400m休息12分钟,而乌龟不休息一直跑。
@start 7:
@end 7:56
* */
public class Seventeen_RabitAndT {
public static void main(String[] args){
RabitAndT rat=new RabitAndT();
rat.init();
rat.start();
}
}
class RabitAndT {
Thread r=null;
Thread t=null;
int rRun=0;
int rSleep=0;
int rMax=6000;
int tMax=6000;
public void init(){
r=new Thread(){
public void run(){
while(rMax>0){
if(rRun<8){
rRun++;
rMax-=300;
try{
System.out.println("rabbit run:"+rRun);
System.out.println("rabbit'finished line distance:`"+rMax);
this.sleep(1000);
}
catch(Exception exp){}
}
else{
rRun=0;
try{
System.out.println();
System.out.println("rabbit need to sleep 12s:`"+new Date());
this.sleep(12000);
System.out.println("rabbit wake @ :`"+new Date());
System.out.println();
}
catch(Exception exp){}
}
}
System.out.println("------------------- rabit finished!");
}
};
t=new Thread(){
public void run(){
while(tMax>0){
tMax-=100;
try{
System.out.println(" torest run:"+new Date());
System.out.println(" torest'finished line distance:`"+tMax);
this.sleep(1000);
}
catch(Exception exp){}
}
System.out.println(" -------------------torest finished");
}
};
}
public void start(){
r.start();
t.start();
}
}