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();
}
为什么第一种能够正确去关闭 而第二种不能关闭线程