简体   繁体   中英

How Keep JVM Alive Until All Threads Are Completed?

I have a tool running, and is invoked through a BAT file in Command Prompt. In the tool, the Quartz scheduler periodically invokes a method, which performs the logic.

As per current implementation, all this is done through a single thread, thus even if the CTRL+C is performed on the BAT file, the tool is not shutdown until the process completes.

But, after looking at the process, we came up with the idea of performing part of the process in a separate thread. Here, the main thread would create a new thread, which would go on to perform some time consuming operations, while the parent thread dies. And another flow might be started by the Quartz even before the child thread has ended.

Thus, at a time, there can be only one parent thread, which the Quartz creates, but there can be any number of child threads operating.

In the above scenario, if CTRL+C is performed, then the Quartz scheduler only waits for the parent thread to complete before shutting down, and all other threads are left incomplete.

I would really appreciate it if someone can tell me how can keep the tool running until all threads have completed.

Thanks in advance.

PS: I have tried attaching the newly created child threads as shutdown hooks, but it doesn't seem to solve my problem. Maybe I am doing it wrong, but it hasn't helped me as of yet.

The child threads should not be daemon threads (Thread.setDaemon(false)) , so they will keep the JVM alive.

In case they are not daemon threads, you could wait for all of them to terminate:
- wait for one thread using Thread.join()
- wait for n threads by passing them the same CountDownLatch(n) instance; each child thread will do countDown() on it when finished its work; parent thread should do await() on it

How did you used ShutdownHook? (I have used this functionality for the same purpose and it was working fine for me) have a look on this Link(Useful Example of ShutdownHook)

Here is the pseudo code. (its just for the basic understanding.. your actual implementation will be lot more complex than this)

public class MyClass implements Runnable
{
    public static void main(String[] args) 
        {
        public static boolean isRunning = false;
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() { 
                while(isRunning)
                    Thread.sleep(100);
            }
        });
        Thread t= new Thread (new MyClass());
    }

    public void run(){
        isRunning = true;
        //Do your all work
        isRunning = false;
    }
}

And I will recommend to add ShutdownHook compare to other ways (eg, Thread.join() or creating user thread etc.) As this will give you a proper control. Also, through Shutdownhook, you can notify your worker thread to stop further processing (if required). Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM