我有以下代码:
public class Driver {
private ExecutorService executor = Executors.newCachedThreadPool();
public static void main(String[] args) {
Driver d = new Driver();
d.run();
}
private void run() {
final Timer timer = new Timer();
final TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("Task is running!");
}
};
Runnable worker = new Runnable() {
@Override
public void run() {
timer.scheduleAtFixedRate(task, new Date(), 5 * 1000);
}
};
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Shutdown hook is being invoked!");
try {
if(executor.awaitTermination(20, TimeUnit.SECONDS))
System.out.println("All workers shutdown properly.");
else {
System.out.println(String.format("Maximum time limit of %s reached " +
"when trying to shut down workers. Forcing shutdown.", 20));
executor.shutdownNow();
}
} catch (InterruptedException interrupt) {
System.out.println("Shutdown hook interrupted by exception: " +
interrupt.getMessage());
}
System.out.println("Shutdown hook is finished!");
}
});
executor.submit(worker);
System.out.println("Initializing shutdown...");
}
}当它运行时,我得到以下控制台输出:
Initializing shutdown...
Task is running!
Task is running!
Task is running!
Task is running!
Task is running!
Task is running!
Task is running!
... (this keeps going non-stop)当我运行这个程序时,应用程序永远不会终止。相反,每隔5秒,我就会看到一个新的println“任务正在运行!”。我希望主线程到达main方法的末尾,打印“初始化关闭.”,调用添加的关闭钩子,杀死执行器,最后打印出“关闭钩子完成了!”。
相反,“任务正在运行”只是不断地被打印,程序永远不会终止。这里发生了什么事?
发布于 2013-07-07 21:26:06
我不是专家,但AFAIK您必须终止所有非守护进程线程,以便关机钩子“启动”。在最初的示例中,有3个非守护进程:
Timer(true)来覆盖它。因此,我认为您应该创建一个ThreadFactory,并在初始化“executor”时使用它。
创建一个类,它将是ThreadFactory
private class WorkerThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "Worker");
t.setDaemon(true);
return t;
}
}-当然,重要的是setDaemon :)
将它的一个实例作为参数传递给newCachedThreadPool方法:
private ExecutorService executor = Executors.newCachedThreadPool(new WorkerThreadFactory());应用这两个变化对我来说很有意义,我不得不:
Maximum time limit of 20 reached when trying to shut down workers. Forcing shutdown.
Shutdown hook is finished!希望能帮上忙
伊齐克
golan2@hotmail.com
发布于 2013-07-05 02:46:20
它不是关闭,因为Timer()创建并启动了一个非守护进程线程.然后就再也不会停止了。
有两件事可以导致JVM自行关闭:
System.exit() (或Runtime.halt())的调用由于您已经创建了第二个非守护进程线程(除了运行main()的线程之外),第二个条件将无法满足。
https://stackoverflow.com/questions/17480280
复制相似问题