public class Solution {
public static Thread.UncaughtExceptionHandler handler = new OurUncaughtExceptionHandler();
public static void main(String[] args) {
TestedThread commonThread = new TestedThread(handler);
Thread threadA = new Thread(commonThread, "Нить 1");
Thread threadB = new Thread(commonThread, "Нить 2");
threadA.start();
threadB.start();
threadA.interrupt();
threadB.interrupt();
}
public static class TestedThread extends Thread {
public TestedThread(Thread.UncaughtExceptionHandler handler) {
setDefaultUncaughtExceptionHandler(handler);
start();
}
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException x) {
throw new RuntimeException("My exception message");
}
}
}
public static class OurUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println(t.getName() + ": " + e.getMessage());
}
}
}
объясните пожалуйста, почему так работает, что
setDefaultUncaughtExceptionHandler(handler);
срабатывает и выводит два сообщения согласно двум заданным нитям
а если установить
settUncaughtExceptionHandler(handler);
но при этом handler в любом случае устанавливается для каждой нити.