AddShutdownHook and setUncaughtExceptionHandler not working properly in java

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(...); //Do the same thing for b, c, d...
   while(true){
   if (a.isActive()){
     if (running)
        thread a= new thread(...);
     a.start();
     running=true;
   }
   Thread.sleep(1000); //try catch block...
}

, , , shutdownThread , , , , , . , , , shutdownThread, . , , .

? - ?

setUncaughtExceptionHandler . , . . :

    public static class ErrHandler implements Thread.UncaughtExceptionHandler{
    public final void uncaughtException(Thread t, Throwable e) {
            Error(t + "died, threw exception: " + e);
        }
    }//this is in public class globals

,

producer.setUncaughtExceptionHandler(Globals.errhandler);

e.printStack() . , , . . , . , .

, , , , - - , , , . , , ( , , , , ), .

+3
1

, , . ExceptionHandler.

, Unit-Tests . .

ScheduledExecutorService , ScheduledExecutorService Runnable/Callable Runnable/Callable, .

0

Source: https://habr.com/ru/post/1734532/


All Articles