次のコードがあります。
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 秒ごとに「Task is running!」という新しい println が表示されます。メイン スレッドがメソッドの最後に到達し、「Initializing shutdown...」を出力し、追加されたシャットダウン フックを呼び出し、executor を強制終了し、最後に「Shutdown hook is finished!」を出力すると予想していました。main
代わりに、「タスクは実行中です」というメッセージが表示され続け、プログラムが終了することはありません。何が起きてる?