0

I have a runnable jar file in which I run the Start.java file.

Start.java spawns 3 child threads. How can I kill the Start thread & the child threads?

Here is Start.java

public class Start {

    public static void main(String[] args){
        ListenersManager.start();
        PollPatientPortalManager.start();
        PollHISManager.start();
    }
}

ListenersManager, PollPatientPortalManager & PollHISManager all extend Thread.

Abhishek Modi
  • 187
  • 4
  • 17

1 Answers1

1

The start thread is the main thread. To "kill" it, you just need to return from main().

For the other threads, you can call .destroy() and .stop() on them. Note that these methods are rude and can cause bugs (that's why they are deprecated).

The correct solution is to define a "signal" (like a shared flag) which the threads check once in a while. To stop them, send the signal.

To kill all threads from the command line, just kill the process (see man kill).

Related:

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • But the thing is I will make a runnable jar. I will run the start thread on jar startup. But there is no way to stop the child threads from the jar itself. So the only way is through the command line. I want to write a script for stopping the threads. Is there a way to do that? – Abhishek Modi Jul 19 '14 at 13:38
  • The clean way is to listen to a signal in the `main` thread. For example, you can open a socket and wait for connections. A command line tool can then connect to the socket and send commands, ask for the current status, etc. That way, you can cleanly shut down. – Aaron Digulla Jul 21 '14 at 08:49