. 1 Package Mountain; 2 / ** . 3 * Attribute: climbing 100 m long (time) 100 m number (NUM); . 4 * simulated climbing thread sleep latency . 5 * @author L . 6 * . 7 * / . 8 public class ClimbThread the implements {the Runnable . 9 10 Private int Time; . 11 Private int NUM = 10 ; 12 is 13 is public ClimbThread ( int Time) { 14 // the TODO Auto-Generated Method Stub 15 the 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 }