0

I would like to close all threads that are running (if there are) when my main method is called.

public static void main(String[] args) throws Exception {
  // Close all thread except current one
  ...
  // Remaining of my program
}
Nans
  • 146
  • 2
  • 4
  • 2
    That sounds like someone split a problem into two parts, solved the easy one and is now struggling with the hard part. **What** are you really trying to solve there? There certainly is a better solution out there. – Voo Feb 29 '12 at 07:53
  • 2
    It sounds like what ever you are trying to do a) doesn't need to be done b) is better done another way. Can you explain why you believe you need this, or is it just an exercise? – Peter Lawrey Feb 29 '12 at 08:13
  • Actually, I need to restart my program at some point. So I tried a different way right now : Thread that can detect that program should restart interrupts itself. Then in main, I detect if this Thread is interrupted and close all other Threads before calling main() again. Problem is that one of my Thread is currently waiting on System.in and then does not accept to be interrupted. – Nans Feb 29 '12 at 10:00

4 Answers4

8

First of all, are you aware that there are some threads in the JVM that are started automatically and you should never mess around with them? These include Finalizer thread, various JMX threads, Swing's EDT, Reference Handler, etc.

Secondly, are you aware that you cannot just stop a thread in Java safely? You can only ask it gently to stop.

If you are aware of the above, try iterating over all threads in the JVM (see: Get a List of all Threads currently running in Java) and call interrupt() on each of them excluding your main thread. Please do not use stop() as this may lead to very nasty problems like deadlocks.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I know all that. And you are right, I am not doing it the right way like that. But I think I found a beginning of a workaround in interrupting threads themselves when possible and detecting that in main class. – Nans Feb 29 '12 at 10:03
1

If some of your threads are performing blocking operation (i.e. don't react to Thread#interrupt()), then it's impossible to close them before the operation is complete, short of killing the JVM. Sorry.

1

The direct answer to your question is to call getThreadGroup on the current thread to get the thread to which the current thread belongs and then enumerate over the other threads and then call interrupt on each one.

The real answer is you should NEVER do this for the reasons specified in the answer provided by Tomasz.

stepanian
  • 11,373
  • 8
  • 43
  • 63
0

This is how I fixed my problem. First, I interrupt the thread that can be itself interrupted :

Thread.currentThread().interrupt();

In main class (or thread), I detect that my previous thread is not alive anymore and then close the last living thread :

Thread trx = new Thread(clTh);
Thread ttx = new Thread(csTh);

trx.start();
ttx.start();
while(trx.isAlive()) {
    Thread.currentThread().sleep(1000);
}

while(ttx.isAlive()) {
    ttx.interrupt();
    Thread.currentThread().sleep(1000);
}

Then in my second thread, that was waiting for a in.readLine(), I handle it that way, testing in.ready().

Community
  • 1
  • 1
Nans
  • 146
  • 2
  • 4