• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Multi Threading

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code. Wants the main thread to wait until all other threads finishes execution.

Thank you
Garandi
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic