多人爬山(多线程 )

编写

 

package com.Input_Output.I_O2;

/**
 *     测试类
 */
class ClimbTest{
    public static void main(String[] args) {
        //创建线程
        ClimbThread t1=new ClimbThread(500,1);
        ClimbThread t2=new ClimbThread(800,1);
        
        //给线程赋名
        t1.setName("□少壮不努力");
        t2.setName("■老大徒伤悲");
        
        //start运行
        t1.start();
        t2.start();
    }
}

/**
 *      爬山对象 线程
 */
class ClimbThread extends Thread{
    int time;                //速度/米
    int num;                //每次休息时间
    int mountain;            //山的高度
    
    public ClimbThread(int time,int mountain) {
        super();
        this.time=time;
        this.mountain = mountain*1000;                    //千米
    }
    //run方法
    public void run() {
        String name=Thread.currentThread().getName();    //获取当前线程名
        while(true) {
            mountain-=100;                                //爬山
            System.out.println(name+"爬了"+(num+1)+"个100米");
            num++;                                        //爬100米次数+1
            try {
                Thread.sleep(time);                        //爬100用的时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(mountain<=0) {
                System.out.println("***"+name+"爬到了终点***");
                break;
            }
        }
        
    }
}

 

 

 

运行

 

转载于:https://www.cnblogs.com/zzh630/p/10500414.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值