Skip to content

Instantly share code, notes, and snippets.

@megascus
Created June 3, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megascus/0961698e426bf7d1f23b2f7be9be8210 to your computer and use it in GitHub Desktop.
Save megascus/0961698e426bf7d1f23b2f7be9be8210 to your computer and use it in GitHub Desktop.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class UncaughtExceptionHandlerSample {
public static void main(String[] args) throws NoSuchFieldException, InterruptedException {
//Threadに設定する場合
ExecutorService threadCase = Executors.newFixedThreadPool(1, new LoggingThreadFactory());
threadCase.execute(() -> {
throw new RuntimeException("例外1");
});
//ThreadGroupで設定する場合
ExecutorService threadGroupCase = Executors.newFixedThreadPool(1, new LoggingByThreadGroupThreadFactory());
threadGroupCase.execute(() -> {
throw new RuntimeException("例外2");
});
//アプリケーション全体に設定する場合
Thread.UncaughtExceptionHandler dueh = Thread.getDefaultUncaughtExceptionHandler();
if (dueh == null) {
Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {
System.out.println("これはデフォルトのUncaughtExceptionHandlerで出力されています。");
System.out.println("ThreadGroup=" + t.getThreadGroup().getName() + "," + "Thread=" + t.getName());
e.printStackTrace();//適切なロガーで出力してください。
});
}
//アプリケーション全体に設定されてもスレッドやスレッドグループで設定されている場合は呼び出されない
threadCase.execute(() -> {
throw new RuntimeException("例外3");
});//ThreadのUncaughtExceptionHandlerが使用される。
threadGroupCase.execute(() -> {
throw new RuntimeException("例外4");
});//ThreadGroupが使用される。
throw new RuntimeException("例外5"); //DefaultUncaughtExceptionHandlerが使用される。
}
}
class LoggingThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setUncaughtExceptionHandler(new UncaughtExceptionHandlerImpl());
return t;
}
private class UncaughtExceptionHandlerImpl implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("これはスレッドに設定されたUncaughtExceptionHandlerで出力されています。");
System.out.println("ThreadGroup=" + t.getThreadGroup().getName() + "," + "Thread=" + t.getName());
e.printStackTrace();//適切なロガーで出力してください。
}
}
}
class LoggingByThreadGroupThreadFactory implements ThreadFactory {
private static final ThreadGroup loggingThreadGroup = new LoggingThreadGroup("loggingThreadGroup");
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(loggingThreadGroup, r); //スレッドを作成するときにスレッドグループを渡すことでそのスレッドグループに所属するスレッドになる。
return t;
}
static class LoggingThreadGroup extends ThreadGroup {
public LoggingThreadGroup(String name) {
super(name);
}
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("これはスレッドグループで出力されています。");
System.out.println("ThreadGroup=" + t.getThreadGroup().getName() + "," + "Thread=" + t.getName());
e.printStackTrace();//適切なロガーで出力してください。
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment