【0805作业】模拟多人爬山

 1 package mountain;
 2 /**
 3  * 属性:爬100米时长(time) 多少个100米(num);
 4  * 线程休眠模拟爬山延时
 5  * @author L
 6  *
 7  */
 8 public class ClimbThread implements Runnable{
 9     
10     private int time;
11     private int num=10;
12     
13     public   ClimbThread(int time) {
14         // TODO Auto-generated method stub
15         this.time=time;
16     }
17     @Override
18     public void run() {
19         // TODO Auto-generated method stub
20         for(int i=num-1;i>=0;i--) {//最高1000米
21             Thread t=Thread.currentThread();
22             System.out.println(Thread.currentThread().getName()+"爬完100米");
23             if(i==0) {
24                 System.out.println(Thread.currentThread().getName()+"到达终点");
25                 break;
26             }
27             
28         try {
29             Thread.sleep(time);
30         } catch (InterruptedException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34         
35         }
36     }
37 
38 }
 1 package mountain;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         ClimbThread ct=new ClimbThread(1000);
 6         ClimbThread ct2=new ClimbThread(2000);
 7         Thread young=new Thread(ct,"年轻人");
 8         Thread old =new Thread(ct2,"老年人");
 9         
10         young.start();
11         old.start();
12     }
13 }

猜你喜欢

转载自www.cnblogs.com/yanglanlan/p/11304248.html