我有以下如何從不同的線程「捕獲」System.exit(-1)?
System.setSecurityManager(new SecurityManager() {
@Override
public void checkExit(int status) {
super.checkExit(status);
//custom exception extends SecurityException
throw new SystemExitCalledException();
}
});
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
//never reaches here somehow...
//maybe some3rdPartyStaticMethod sets this, too?
}
});
try {
//this method spawns threads and some call System.exit().
some3rdPartyStaticMethod.invoke(null, new Object[]{args});
} catch (SystemExitCalledException e) {
//it never reaches here because
//SystemExitCalledException is thrown on different thread??
//
}
是否有辦法防止some3rdPartyStaticMethod的產生的線程的System.exit()的距離停止JVM電話嗎?
也許您可以使用'SecurityManager'將'CallException.exit'的調用替換爲'SecurityException'。 – 2012-04-14 13:08:42