aspose file tools*
The moose likes Threads and Synchronization and the fly likes Multi Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multi Threading" Watch "Multi Threading" New topic
Author

Multi Threading

Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
I have the following code. Wants the main thread to wait until all other threads finishes execution.

Thank you
Garandi
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
The pure JFC solution is to store all the threads in a collection and have the main thread call join() on each one after they're all launched.

If you have util.concurrent in the classpath anyway you can use CountDown or in Java 5.0 CountDownLatch
[ August 04, 2004: Message edited by: David Weitzman ]
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
The following modification has the main thread join each sub thread which will cause the main thread to wait for each sub thread to complete.

David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Rovas, calling Thread.start() on one line and Thread.join() on the same thread a line later is not the most useful of patterns. It's like running the code without starting a thread, only there's a lot more overhead. I'm having trouble imagining circumstances when you'd ever want to join() immediately after start()ing. I guess it would keep the code from mucking with your ThreadLocals or throwing unexpected Errors/RuntimeExceptions at you.

I've added a word to my response above to clarify.
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
Thanks for pointing that out, David. The code that I submitted is not multi-threaded at all. As stated in the first reply(I'd use the user name if i could see - come on javaranch), I should put each sub-thread in a collection and then call join on each thread.
Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192

I should put each sub-thread in a collection and then call join on each thread.

I was wondering how to code it, if you can help me with it; that would be very useful.
Thank you in advance
Garandi
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi Threading
 
Similar Threads
Dan's Thread
How do you evaluate getTime() repeatedly?
How long a thread has been running
Bug in code, need an outside perspective - cannot figure it out.
Measuring Time in Multi-threaded Application Using "System.currentTimeMillis()"