1
Java代碼如何停止線程當Tomcat停止
static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 0l, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
threadPoolExecutor.execute(customer);
class Customer implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
tomcat的停止線,但仍然活着;
如何在tomcat停止時停止線程?
調用ThreadPool。[shutdownNow()](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#shutdownNow())方法 – Stefan