File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes how to end all threads before ending main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "how to end all threads before ending main" Watch "how to end all threads before ending main" New topic
Author

how to end all threads before ending main

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

The easiest way is Thread.join().


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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

Joined: Jan 30, 2000
Posts: 18671
[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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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

Joined: Jan 30, 2000
Posts: 18671
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to end all threads before ending main
 
Similar Threads
RMI server stops
Killing a parent thread
Threads - notify() and wait()
Main thread and child thread
Aboutt Daemon Threads