package testThread;
import java.util.Random;
class tuzi implements Runnable {
public volatile boolean exit = false;
public void run() {
int run = 0;
for(int i=0;i<15;i++){
while(!exit){
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
Random rand = new Random();
int randNum = rand.nextInt(20);
run = run+randNum;
System.out.println(Thread.currentThread().getName()+"跑了"+run+"米");
if(run>100){
exit = true;
System.out.println("兔子跑到终点了");
}
}
}
}
}
class wugui implements Runnable {
public volatile boolean exit = false;
public void run() {
int run = 0;
for(int i=0;i<40;i++){
while(!exit){
try {
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
Random rand = new Random();
int randNum = rand.nextInt(6);
run = run+randNum;
System.out.println(Thread.currentThread().getName()+"跑了"+run+"米");
if(run>100){
exit = true;
System.out.println("乌龟跑到终点了");
}
}
}
}
}
public class Thread6 {
public static void main(String[] args) {
new Thread(new tuzi(),"兔子").start();
new Thread(new tuzi()).setPriority(Thread.MIN_PRIORITY);
new Thread(new wugui(),"乌龟").start();
}
}