多线程龟兔赛跑

package com.zhidi.lianxi;


public class Lianxi02 {


public static void main(String[] args) {
Gui g = new Gui();
g.setName("乌龟");
g.setPriority(5);
Tu t = new Tu();
t.setName("兔子");
t.setPriority(5);

g.start();
t.start();


}


}


class Gui extends Thread{


@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"起跑:");
for (int i = 1; i <= 1000; i++) {
System.out.println(Thread.currentThread().getName()+"跑了"+i+"米");
}
System.out.println(Thread.currentThread().getName()+"到达了终点!");
}

}


class Tu extends Thread{


@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"起跑:");
for (int i = 1; i <= 1000; i++) {
System.out.println(Thread.currentThread().getName()+"跑了"+i+"米");
if(i==500){
try {
System.out.println(Thread.currentThread().getName()+"兔子看到乌龟跑得太慢,睡了一会。");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName()+"到达了终点!");
}

}

你可能感兴趣的:(案例)