package com.kidd.baiduzhidao;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Tortoise tortoise = new Tortoise();
tortoise.setLength(50000);
Rabbit rabbit = new Rabbit();
rabbit.setLength(50000);
Thread t1 = new Thread(tortoise);
Thread t2 = new Thread(rabbit);
t1.start();
t2.start();
}
}
class Rabbit implements Runnable {
private int length;
private int index = 0;
private Date endTime;
private Date startTime;
private long consumingTime;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public Date getEndTime() {
return endTime;
}
public Date getStartTime() {
return startTime;
}
public long getConsumingTime() {
return consumingTime;
}
@Override
public void run() {
this.startTime = new Date();
while (this.length > 0) {
index++;
this.length = this.length - 100;
if (index % 100 == 0) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
this.endTime = new Date();
this.consumingTime = this.endTime.getTime() - this.startTime.getTime();
System.out.println("兔子耗时:" + consumingTime + "ms.");
}
}
class Tortoise implements Runnable {
private int length;
private Date endTime;
private Date startTime;
private long consumingTime;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public Date getEndTime() {
return endTime;
}
public Date getStartTime() {
return startTime;
}
public long getConsumingTime() {
return consumingTime;
}
@Override
public void run() {
this.startTime = new Date();
while (this.length > 0) {
this.length = this.length - 1;
}
this.endTime = new Date();
this.consumingTime = this.endTime.getTime() - this.startTime.getTime();
System.out.println("乌龟耗时:" + consumingTime + "ms.");
}
}