0309作业
第一题
1 package com.kgc.zuoye.zy0309.zy02; 2 //爬山 3 public class Test { 4 public static void main(String[] args){ 5 Thread t=new Thread(new MyRunnable(500,1),"kkk"); 6 Thread t1=new Thread(new MyRunnable(800,1),"222"); 7 8 t.start(); 9 t1.start(); 10 } 11 }
1 package com.kgc.zuoye.zy0309.zy02; 2 3 import util.Time; 4 5 public class MyRunnable implements Runnable { 6 private int time; 7 private int num; 8 9 public MyRunnable(int time, int num) { 10 this.time = time; 11 this.num = num; 12 } 13 14 @Override 15 public void run() { 16 int sum = 0; 17 while (sum < 1000) { 18 sum = num * 100; 19 System.out.println(Thread.currentThread().getName() + "爬了" + sum + "米"); 20 num++; 21 try { 22 Thread.sleep(this.time); 23 } catch (InterruptedException e) { 24 e.printStackTrace(); 25 } 26 } 27 System.out.println(Thread.currentThread().getName() + "达到山顶"); 28 } 29 }
运行结果

第二题
运行结果
第三题
运行结果
第四题
运行结果

浙公网安备 33010602011771号