1 // 登山機能実装Runnableを実装クラス 2つの パブリック クラスは ClimbThread 実装のRunnable { 3。 プライベート int型の時間; 4 プライベート INT NUM = 5 ; 5 。6 パブリック ClimbThread(INTの時間){ 7 この .time = 時間; 8 } 9。 10 パブリック ボイドRUN(){ 11 ながら(真の){ 12は、 IF(NUM == 0 ){ 13であります System.out.println(Thread.currentThread().getName()+"爬完了!"); 14 break; 15 } 16 System.out.println(Thread.currentThread().getName()+"爬完100米!"); 17 try { 18 Thread.sleep(time); 19 } catch (InterruptedException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 num--; 24 } 25 26 } 27 }
1 //测试类 2 public class Test { 3 public static void main(String[] args) { 4 ClimbThread user = new ClimbThread(500); 5 ClimbThread user1 = new ClimbThread(1200); 6 Thread thread = new Thread(user,"年轻人"); 7 Thread thread1 = new Thread(user1,"老年人"); 8 thread.start(); 9 thread1.start(); 10 } 11 }
运行结果: