Javaマルチスレッドのキャプチャ異常
8176 ワード
1.メインスレッドがサブスレッドの例外を取得できない
出力:
2.HandlerThreadFactoryの設定による異常のキャプチャ
出力:
3.デフォルトの例外取得クラスによる例外の取得
出力:
package Thread.Exection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExeceptionThread implements Runnable {
@Override
public void run() {
throw new RuntimeException();
}
public static void main(String[] args) {
try {
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new ExeceptionThread());
} catch (Exception e) {
System.out.println("exeception has handled");
}
}
}
出力:
Exception in thread "pool-1-thread-1" java.lang.RuntimeException
at Thread.Exection.ExeceptionThread.run(ExeceptionThread.java:10)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
2.HandlerThreadFactoryの設定による異常のキャプチャ
package Thread.Exection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
class ExceptionThread2 implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.println("run() by " + t);
System.out.println("eh=" + t.getUncaughtExceptionHandler());
throw new RuntimeException();
}
}
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("caught " + e);
}
}
class HandlerThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
System.out.println(this + " creating new Thread");
Thread t = new Thread(r);
System.out.println("created " + t + " ID:" + t.getId());
t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
System.out.println("eh=" + t.getUncaughtExceptionHandler());
return t;
}
}
public class CaptureUncaughtException {
public static void main(String[] args) {
ExecutorService exec = Executors
.newCachedThreadPool(new HandlerThreadFactory());
exec.execute(new ExceptionThread2());
}
}
出力:
Thread.Exection.HandlerThreadFactory@4e25154f creating new Thread
created Thread[Thread-0,5,main] ID:9
eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4e
run() by Thread[Thread-0,5,main]
eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4e
Thread.Exection.HandlerThreadFactory@4e25154f creating new Thread
created Thread[Thread-1,5,main] ID:11
eh=Thread.Exection.MyUncaughtExceptionHandler@193fa958
caught java.lang.RuntimeException
3.デフォルトの例外取得クラスによる例外の取得
package Thread.Exection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SettingDefaultHandler {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(
new MyUncaughtExceptionHandler());
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new ExceptionThread2());
}
}
出力:
run() by Thread[pool-1-thread-1,5,main]
eh=java.lang.ThreadGroup[name=main,maxpri=10]
caught java.lang.RuntimeException