爬山
作者:互联网
package com.day18; public class Test { public static void main(String[] args) { Demo02 youngMan = new Demo02("年轻人",500,1); Demo02 oldMan = new Demo02("老年人",1500,1); System.out.println("********开始爬山*********"); youngMan.start(); oldMan.start(); } }
package com.day18; public class Demo02 extends Thread { private int time; // 爬100米的时间 public int num = 0; // 爬多少个100米 public Demo02(String name, int time, int kilometer) { super(name); this.time = time; this.num = kilometer * 1000 / 100; } public void run() { while (num > 0) { try { Thread.sleep(this.time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "爬完100米!"); num--; } System.out.println(Thread.currentThread().getName()+"到达终点!"); } }
标签:Thread,int,爬山,Demo02,num,time,public 来源: https://www.cnblogs.com/zhangbupang/p/11305764.html