• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to end all threads before ending main

 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main() creates a thread.
That thread spawns multiple threads which may also spawn threads of their own.
Is there a simple way to keep main from ending before all the threads have ended?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way is Thread.join().
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try

The other option is to use join() on each (non-daemon) thread that's been spawned. If you can ensure that all new threads are added to a Queue (or synchronized LinkedList if you're in 1.4) before they start(), then you can do something like this:

If you don't control all the code that's spawning threads, and can't ensure that new threads get added to allThreads, then it gets messier. It's possible to use Thread and ThreadGroup to locate all threads running in the JVM and join them all, but it gets messy. And you can't guarantee that new threads weren't added while you were waiting for other threads, so you'd have to keep re-running the method until it found that there were no other (non-daemon) threads. All in all, the shutdown hook's probably the way to go.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I need to add a shutdown hook to each thread as I start that thread? The javadoc sounds like I need to call System.exit to initiate the shutdown hook(s) to activate.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[MdQ]: Do I need to add a shutdown hook to each thread as I start that thread?

No - shutdown hooks aren't added to individual threads, they're added to the whole JVM, and they activate only when the whole JVM is shutting down.

[MdQ]: The javadoc sounds like I need to call System.exit to initiate the shutdown hook(s) to activate.

No, System.exit() is one of the ways a shutdown hook can be activated - but it will also run on a normal exit when all (other) non-daemon threads have finished running.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will also run on a normal exit when all (other) non-daemon threads have finished running.

Is it guaranteed to run only after all the other non-daemon threads have finished running?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shutdown hooks run when the JVM would otherwise shut down. The JVM will not shut down while non-daemon threads remain alive, unless a specific event causes a shutdown, such as System.exit(), ctrl-C from a user, a kill command from the system, or something similar.

Note that shutdown threads are themselves typically non-daemon threads - but it isn't started until the shutdown sequence begins. So what happens is, in a normal exit, the JVM waits until all previously-started non-daemon threads complete, then it starts a new thread (typically non-daemon) for each shutdown hook that has been added. Once all these complete, the system exits. So there can be one or more non-daemon threads after shutdown begins, but those are only the shutdown hooks themselves. None of the other previously-running non-daemon threads may still be alive at this point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic