|
|
|
@ -36,7 +36,7 @@ class SilentExitExceptionHandler implements UncaughtExceptionHandler {
|
|
|
|
|
@Override
|
|
|
|
|
public void uncaughtException(Thread thread, Throwable exception) {
|
|
|
|
|
if (exception instanceof SilentExitException) {
|
|
|
|
|
if (jvmWillExit(thread)) {
|
|
|
|
|
if (isJvmExiting(thread)) {
|
|
|
|
|
preventNonZeroExitCode();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
@ -46,19 +46,7 @@ class SilentExitExceptionHandler implements UncaughtExceptionHandler {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setup(Thread thread) {
|
|
|
|
|
UncaughtExceptionHandler handler = thread.getUncaughtExceptionHandler();
|
|
|
|
|
if (!(handler instanceof SilentExitExceptionHandler)) {
|
|
|
|
|
handler = new SilentExitExceptionHandler(handler);
|
|
|
|
|
thread.setUncaughtExceptionHandler(handler);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void exitCurrentThread() {
|
|
|
|
|
throw new SilentExitException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean jvmWillExit(Thread exceptionThread) {
|
|
|
|
|
private boolean isJvmExiting(Thread exceptionThread) {
|
|
|
|
|
for (Thread thread : getAllThreads()) {
|
|
|
|
|
if (thread != exceptionThread && thread.isAlive() && !thread.isDaemon()) {
|
|
|
|
|
return false;
|
|
|
|
@ -67,22 +55,15 @@ class SilentExitExceptionHandler implements UncaughtExceptionHandler {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void preventNonZeroExitCode() {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Thread[] getAllThreads() {
|
|
|
|
|
ThreadGroup rootThreadGroup = getRootThreadGroup();
|
|
|
|
|
int size = 32;
|
|
|
|
|
int threadCount;
|
|
|
|
|
Thread[] threads;
|
|
|
|
|
do {
|
|
|
|
|
size *= 2;
|
|
|
|
|
threads = new Thread[size];
|
|
|
|
|
threadCount = rootThreadGroup.enumerate(threads);
|
|
|
|
|
Thread[] threads = new Thread[32];
|
|
|
|
|
int count = rootThreadGroup.enumerate(threads);
|
|
|
|
|
while (count == threads.length) {
|
|
|
|
|
threads = new Thread[threads.length * 2];
|
|
|
|
|
count = rootThreadGroup.enumerate(threads);
|
|
|
|
|
}
|
|
|
|
|
while (threadCount == threads.length);
|
|
|
|
|
return Arrays.copyOf(threads, threadCount);
|
|
|
|
|
return Arrays.copyOf(threads, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ThreadGroup getRootThreadGroup() {
|
|
|
|
@ -93,6 +74,22 @@ class SilentExitExceptionHandler implements UncaughtExceptionHandler {
|
|
|
|
|
return candidate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void preventNonZeroExitCode() {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setup(Thread thread) {
|
|
|
|
|
UncaughtExceptionHandler handler = thread.getUncaughtExceptionHandler();
|
|
|
|
|
if (!(handler instanceof SilentExitExceptionHandler)) {
|
|
|
|
|
handler = new SilentExitExceptionHandler(handler);
|
|
|
|
|
thread.setUncaughtExceptionHandler(handler);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void exitCurrentThread() {
|
|
|
|
|
throw new SilentExitException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class SilentExitException extends RuntimeException {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|