【爬山类】每个线程代表一个人,可设置每人爬山速度每爬完,100米显示信息爬到终点时给出相应提示
作者:互联网
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();
}
}
标签:Thread,显示信息,爬山,ClimbThread,length,线程,time,100,public 来源: https://blog.csdn.net/mx199836/article/details/122792503