ノーオペレーションですか?JVM はシャットダウン フックの再呼び出しを回避しますか?
ユースケースとして、UncaughtExceptionHandler が SomeException に対して System.exit() を呼び出し、その後 SomeException が短時間のうちに 2 つの別々のスレッドでスローされるとします。
また、潜在的なデッドロックを回避するために、新しいスレッドで System.exit() が呼び出されると仮定します。
アップデート
コメントの1つが正しく指摘したように、私はこれを自分でテストするべきでしたが、怠惰でした. 以下のテストは、 System.exit() が通常のスレッドで呼び出されたかデーモン スレッドで呼び出されたかに関係なく正常に完了し、出力後にコード 1 で終了します。
Requesting shutdown ...
Shutdown started ...
Requesting shutdown ...
Shutdown completed ...
コードは次のとおりです。
public class ConcurrentSystemExit {
private final static boolean DAEMON = false;
public static void main(String[] args) throws InterruptedException {
// Register a shutdown hook that waits 6 secs
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
System.out.println("Shutdown started ...");
Thread.sleep(6000);
System.out.println("Shutdown completed ...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Define a Thread that calls exit()
class ShutdownThread extends Thread {
public void run() {
System.out.println("Requesting shutdown ...");
System.exit(1);
}
}
// Issue first exit()
Thread t1 = new ShutdownThread();
t1.setDaemon(DAEMON);
t1.start();
Thread.sleep(3000);
// Issue second exit()
Thread t2 = new ShutdownThread();
t2.setDaemon(DAEMON);
t2.start();
}
}