As far as I understand this I am setting the default exception handler OurUncaughtExceptionHandler. Placing it inside the TestedThread class definition.
It outputs the two lines correctly. Thus, it must be using "OurUncaughtExceptionHandler to display the two messages.
Please help!
package com.codegym.task.task16.task1633;
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, "Thread 1");
Thread threadB = new Thread(commonThread, "Thread 2");
threadA.start();
threadB.start();
threadA.interrupt();
threadB.interrupt();
}
public static class TestedThread extends Thread {
public TestedThread(Thread.UncaughtExceptionHandler handler) {
//setUncaughtExceptionHandler(handler);
setDefaultUncaughtExceptionHandler(new OurUncaughtExceptionHandler());
start();
}
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
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());
}
}
}