【0805作业】模拟多人爬山

本文介绍了一个使用Java实现的爬山模拟程序,通过创建线程并设置不同的爬山时间,来模拟不同速度的登山者。程序中定义了ClimbThread类,实现了Runnable接口,通过线程休眠模拟爬山过程中的延时。

 

 

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值