/***
- 龟兔比赛
*编写龟兔赛跑多线程程序,设置赛跑长度为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;
}
}