格式化代码
This commit is contained in:
@@ -1,42 +1,46 @@
|
||||
package concurrency;//: concurrency/CaptureUncaughtException.java
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
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();
|
||||
}
|
||||
@Override
|
||||
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 {
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
System.out.println("caught " + e);
|
||||
}
|
||||
Thread.UncaughtExceptionHandler {
|
||||
@Override
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
System.out.println("caught " + e);
|
||||
}
|
||||
}
|
||||
|
||||
class HandlerThreadFactory implements ThreadFactory {
|
||||
public Thread newThread(Runnable r) {
|
||||
System.out.println(this + " creating new Thread");
|
||||
Thread t = new Thread(r);
|
||||
System.out.println("created " + t);
|
||||
t.setUncaughtExceptionHandler(
|
||||
new MyUncaughtExceptionHandler());
|
||||
System.out.println(
|
||||
"eh = " + t.getUncaughtExceptionHandler());
|
||||
return t;
|
||||
}
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
System.out.println(this + " creating new Thread");
|
||||
Thread t = new Thread(r);
|
||||
System.out.println("created " + t);
|
||||
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());
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
ExecutorService exec = Executors.newCachedThreadPool(
|
||||
new HandlerThreadFactory());
|
||||
exec.execute(new ExceptionThread2());
|
||||
}
|
||||
} /* Output: (90% match)
|
||||
HandlerThreadFactory@de6ced creating new Thread
|
||||
created Thread[Thread-0,5,main]
|
||||
|
||||
Reference in New Issue
Block a user