package exe;
/**
* ClassName:
* Function:
* Date: 2017年10月11日 下午2:20:23
* @author zhang yi
* @since JDK 1.8
*/
public class Race {
public static void main(String[] args) {
Thread t1 = new Animail();
Thread t2 = new Animail();
t1.setName("tuzi");
t2.setName("wugui");
t1.start();
t2.start();
}
}
class Animail extends Thread{
int num;
public static boolean point = false;
@Override
public void run() {
while (num<50) {
if (point) {
break;
}
if(currentThread().getName().equals("tuzi")){
if (Math.random()<0.3) {
num+=2;
System.out.println("tuzi:"+num);
if (num==50) {
break;
}
}
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if (currentThread().getName().equals("wugui")) {
if (Math.random()>=0.3) {
num++;
System.out.println("wugui:"+num);
if (num==50) {
break;
}
}
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (!point) {
System.out.println(currentThread().getName()+"到达终点");
point=true;
}
}
}
/**
* ClassName:
* Function:
* Date: 2017年10月11日 下午2:20:23
* @author zhang yi
* @since JDK 1.8
*/
public class Race {
public static void main(String[] args) {
Thread t1 = new Animail();
Thread t2 = new Animail();
t1.setName("tuzi");
t2.setName("wugui");
t1.start();
t2.start();
}
}
class Animail extends Thread{
int num;
public static boolean point = false;
@Override
public void run() {
while (num<50) {
if (point) {
break;
}
if(currentThread().getName().equals("tuzi")){
if (Math.random()<0.3) {
num+=2;
System.out.println("tuzi:"+num);
if (num==50) {
break;
}
}
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if (currentThread().getName().equals("wugui")) {
if (Math.random()>=0.3) {
num++;
System.out.println("wugui:"+num);
if (num==50) {
break;
}
}
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (!point) {
System.out.println(currentThread().getName()+"到达终点");
point=true;
}
}
}