I have a multi-threaded program where I have one thread to view multiple threads. The work is done as follows:
The main program initiates and runs Watcher Thread, in void Main (), I have a line
Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownThread(), "Exit Listener"));
When I do not start the observer thread, ShutdownThread is called when I exit the program, but when I start the Watcher thread that has a dead loop, ShutdownThread is not called (I print a message in this thread). It is very strange. Any explanation?
The observer stream is similar:
public void run(){
boolean running=false;
thread a=new thread(...);
while(true){
if (a.isActive()){
if (running)
thread a= new thread(...);
a.start();
running=true;
}
Thread.sleep(1000);
}
, , , shutdownThread , , , , , . , , , shutdownThread, . , , .
? - ?
setUncaughtExceptionHandler . , . . :
public static class ErrHandler implements Thread.UncaughtExceptionHandler{
public final void uncaughtException(Thread t, Throwable e) {
Error(t + "died, threw exception: " + e);
}
}
,
producer.setUncaughtExceptionHandler(Globals.errhandler);
e.printStack() . , , . . , . , .
, , , , - - , , , . , , ( , , , , ), .