public class ClimbThread extends Thread{
int time; // 爬100耗时
int length; // 总长度 赋值1000
@Override
public void run() {
while(length > 0) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
length -= 100;
System.out.println(Thread.currentThread().getName() + "爬了100米,还剩余" + length);
}
System.out.println("恭喜:" + Thread.currentThread().getName() + "爬到了山顶");
}
public ClimbThread(int time,int length,String name) {
super(name);
this.time = time;
this.length = length;
}
public static void main(String[] args) {
ClimbThread youngMan = new ClimbThread(500, 1000, "练习两年半的实习生");
ClimbThread oldMan = new ClimbThread(1000, 1000, "老年人");
youngMan.start();
oldMan.start();
}
}