首页 新闻 会员 周边 捐助

新手在学习多线程中遇到的问题

0
[待解决问题]
1.public void run() {
try {
while (!Thread.interrupted()){
System.out.println("Wax on!");
    TimeUnit.MILLISECONDS.sleep(200);
    car.waxed();
    car.waitForBuffing();
   }
}
catch (InterruptedException e) {
System.out.println("Exiting via interrupt");
}

System.out.println("Ending Wax On task");
}


2.
public void run() {
while (!Thread.interrupted()){
  System.out.println("Wax on!");
  try{
    TimeUnit.MILLISECONDS.sleep(200);
     car.waxed();
    car.waitForBuffing();
    } catch (InterruptedException e) {
   System.out.println("Exiting via interrupt");
  }
}
 System.out.println("Ending Wax On task");
}
}

在主线程中
public static void main(String[] args) throws InterruptedException {
Car car = new Car();
 ExecutorService executorService = Executors.newCachedThreadPool();
 executorService.execute(new WaxOn(car));
 executorService.execute(new WaxOff(car));
 TimeUnit.SECONDS.sleep(3);
 executorService.shutdownNow();
}

为什么第一种能够正确去关闭 而第二种不能关闭线程
水冷的主页 水冷 | 菜鸟二级 | 园豆:202
提问于:2018-06-22 11:09
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册