【0805作业】模拟多人爬山
生活随笔
收集整理的這篇文章主要介紹了
【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 }?
?
轉載于:https://www.cnblogs.com/yanglanlan/p/11304248.html
總結
以上是生活随笔為你收集整理的【0805作业】模拟多人爬山的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: element -ui 表单验证 如
- 下一篇: 다양한 저장매체의 속도를 측정