Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threads, waiting for last one to finish

Reply
Thread Tools

Threads, waiting for last one to finish

 
 
Roedy Green
Guest
Posts: n/a
 
      01-11-2013
I have 25 threads that start at once. I need to wait until the last
one completes. Is there a better way to handle that than using a
ThreadPoolExecutor which seems overkill.
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      01-11-2013
On 1/11/2013 1:56 PM, Roedy Green wrote:
> I have 25 threads that start at once. I need to wait until the last
> one completes. Is there a better way to handle that than using a
> ThreadPoolExecutor which seems overkill.
>


The Executors class has convenience methods to make thread pools easier
to use.

ExecutorService es = Executors.newFixedThreadPool( 25 );
// submit jobs here
es.shutdown();

The shutdown() will wait for all jobs to finish. I don't think you can
get easier than that. It's two lines of code!


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-11-2013
On Fri, 11 Jan 2013 14:05:10 -0800, markspace
<> wrote, quoted or indirectly quoted someone
who said :

>
> ExecutorService es = Executors.newFixedThreadPool( 25 );
> // submit jobs here
> es.shutdown();


Excellent!
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      01-11-2013
On 1/11/2013 2:14 PM, Roedy Green wrote:
> On Fri, 11 Jan 2013 14:05:10 -0800, markspace
> <> wrote, quoted or indirectly quoted someone
> who said :
>
>>
>> ExecutorService es = Executors.newFixedThreadPool( 25 );
>> // submit jobs here
>> es.shutdown();

>
> Excellent!
>


Oops, sorry, it's awaitTermination() that you want, not shutdown().

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      01-12-2013
On 1/11/2013 6:11 PM, markspace wrote:
> On 1/11/2013 2:14 PM, Roedy Green wrote:
>> On Fri, 11 Jan 2013 14:05:10 -0800, markspace
>> <> wrote, quoted or indirectly quoted someone
>> who said :
>>
>>>
>>> ExecutorService es = Executors.newFixedThreadPool( 25 );
>>> // submit jobs here
>>> es.shutdown();

>>
>> Excellent!

>
> Oops, sorry, it's awaitTermination() that you want, not shutdown().


If the Java docs are to be believed he actually wants both.

<quote>
Blocks until all tasks have completed execution after a shutdown
request, or
</quote>

Arne


 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      01-12-2013
On 1/11/2013 1:56 PM, Roedy Green wrote:
> I have 25 threads that start at once. I need to wait until the last
> one completes. Is there a better way to handle that than using a
> ThreadPoolExecutor which seems overkill.
>


You can always join() them.

--

Knute Johnson
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-12-2013
On Fri, 11 Jan 2013 15:11:17 -0800, markspace
<> wrote, quoted or indirectly quoted someone
who said :

>
>Oops, sorry, it's awaitTermination() that you want, not shutdown().


Working great. Thanks.
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-12-2013
On Fri, 11 Jan 2013 15:11:17 -0800, markspace
<> wrote, quoted or indirectly quoted someone
who said :

>
>Oops, sorry, it's awaitTermination()


I think there is a bug. If there are no threads submitted it hangs.
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      01-12-2013
On 01/11/2013 05:56 PM, Roedy Green wrote:
> I have 25 threads that start at once. I need to wait until the last
> one completes. Is there a better way to handle that than using a
> ThreadPoolExecutor which seems overkill.
>

Any reason why you couldn't use CyclicBarrier?

AHS
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      01-12-2013
On 1/12/2013 5:20 AM, Roedy Green wrote:
> On Fri, 11 Jan 2013 15:11:17 -0800, markspace
> <> wrote, quoted or indirectly quoted someone
> who said :
>> Oops, sorry, it's awaitTermination()

>
> I think there is a bug. If there are no threads submitted it hangs.


You meed to call shutdown first.

It is documented.

http://mindprod.com/jgloss/rtfm.html



Arne


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
waiting for multiple child processes to finish Martin DeMello Ruby 1 11-13-2006 02:30 PM
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 2 09-12-2005 11:30 PM
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 0 09-12-2005 06:09 PM
waiting for external process to finish (using Runtime and Thread classes) brownjenkn@aol.com Java 1 04-22-2005 07:55 PM
Waiting for processes to finish under Solaris Behrang Dadsetan Python 1 07-15-2003 07:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57