Threads, waiting for last one to finish

Discussion in 'Java' started by Roedy Green, Jan 11, 2013.

  1. Roedy Green

    Roedy Green Guest

    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.
     
    Roedy Green, Jan 11, 2013
    #1
    1. Advertising

  2. Roedy Green

    markspace Guest

    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!
     
    markspace, Jan 11, 2013
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    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.
     
    Roedy Green, Jan 11, 2013
    #3
  4. Roedy Green

    markspace Guest

    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().
     
    markspace, Jan 11, 2013
    #4
  5. Roedy Green

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Jan 12, 2013
    #5
  6. 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
     
    Knute Johnson, Jan 12, 2013
    #6
  7. Roedy Green

    Roedy Green Guest

    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.
     
    Roedy Green, Jan 12, 2013
    #7
  8. Roedy Green

    Roedy Green Guest

    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.
     
    Roedy Green, Jan 12, 2013
    #8
  9. 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
     
    Arved Sandstrom, Jan 12, 2013
    #9
  10. Roedy Green

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Jan 12, 2013
    #10
  11. Roedy Green

    Roedy Green Guest

    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().


    you need both.
    --
    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.
     
    Roedy Green, Jan 12, 2013
    #11
  12. Roedy Green

    Arne Vajhøj Guest

    On 1/12/2013 1:36 PM, 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() that you want, not shutdown().

    >
    > you need both.


    So the documentation is correct. Surprise. Not!

    Arne
     
    Arne Vajhøj, Jan 12, 2013
    #12
  13. In article <kcq2ap$m4o$>,
    markspace <> wrote:

    > 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!


    The Executor classes are horribly buggy. Pool size adjustment, idle
    timeouts, and shutdown sequences do NOT work. Some of the features are
    impossible to implement efficiently so Sun instead chose to implement
    them incorrectly.

    For an Executor, it would be more correct to iterate through all the
    Future or Callable objects returned and ask for their result.
    --
    I will not see posts from Google because I must filter them as spam
     
    Kevin McMurtrie, Jan 13, 2013
    #13
  14. Roedy Green

    Roedy Green Guest

    On Sat, 12 Jan 2013 08:42:45 -0400, Arved Sandstrom
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Any reason why you couldn't use CyclicBarrier?


    There are a few things that puzzle. What goes in the unshown
    waitUntilDone? just an await? I suppose the mother thread too counts
    as one of the "parties".

    Why does CyclicBarrier have a Runnable? Would it not be simpler to
    have the mother thread invoke a Runnable after the await? or just
    execute the cleanup code on the same thread?

    With CyclicBarrier, you must know the precise number of threads ahead
    of time that will wait. In my case I have an upper bound. Sometimes
    there is nothing to do for a given thread, and I don't even start it.
    To use CyclicBarrier I would need a pre-sweep to get the precise
    count.

    In my case, one of the threads, the one that probes amazon.cn takes
    quite a bit longer than the others. The way it is now, all the
    threads but one shut down early and let the last thread have all the
    resources. With CyclicBarrier they would all be hanging around to the
    last minute.

    Thank you for bringing this option up. I tend use to overuse the first
    tool I encounter out of learning curve fear.

    --
    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.
     
    Roedy Green, Jan 13, 2013
    #14
  15. Roedy Green

    Sebastian Guest

    Am 11.01.2013 22:56, schrieb Roedy Green:
    > 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 could use a Phaser, like so:

    final Phaser phaser = new Phaser(1);
    for(int i=0; i < taskCount; i++) {
    phaser.register();
    executor.execute(new Runnable() {
    public void run() {
    try {
    // do sth
    );
    } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    }
    phaser.arrive();
    }
    });
    }
    phaser.arriveAndAwaitAdvance();


    -- Sebastian
     
    Sebastian, Jan 13, 2013
    #15
  16. On 01/13/2013 03:56 AM, Roedy Green wrote:
    > On Sat, 12 Jan 2013 08:42:45 -0400, Arved Sandstrom
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    >> Any reason why you couldn't use CyclicBarrier?

    >
    > There are a few things that puzzle. What goes in the unshown
    > waitUntilDone? just an await? I suppose the mother thread too counts
    > as one of the "parties".
    >
    > Why does CyclicBarrier have a Runnable? Would it not be simpler to
    > have the mother thread invoke a Runnable after the await? or just
    > execute the cleanup code on the same thread?
    >
    > With CyclicBarrier, you must know the precise number of threads ahead
    > of time that will wait. In my case I have an upper bound. Sometimes
    > there is nothing to do for a given thread, and I don't even start it.
    > To use CyclicBarrier I would need a pre-sweep to get the precise
    > count.
    >
    > In my case, one of the threads, the one that probes amazon.cn takes
    > quite a bit longer than the others. The way it is now, all the
    > threads but one shut down early and let the last thread have all the
    > resources. With CyclicBarrier they would all be hanging around to the
    > last minute.
    >
    > Thank you for bringing this option up. I tend use to overuse the first
    > tool I encounter out of learning curve fear.
    >

    It's this option and also its close cousin the CountdownLatch - useful
    synchronization constructs to be aware of.

    I'll refer you to JCIP for a detailed exposition of the details between
    CountdownLatch and CyclicBarrier - the key difference is that the latch
    is waiting for an event (the latch count becoming 0 through invocations
    of countDown()) while the barrier waits on other threads (these threads
    arriving at the barrier when they call await()).

    In either case, yes, you need to supply the number of threads involved.
    That's to be expected. I have difficulty seeing how you wouldn't be able
    to figure out how many threads you propose to start.

    As for the Runnable for CyclicBarrier, you want to read the Javadoc
    carefully. The barrier is going to run that *after* all threads arrive,
    but *before* they are released - you're welcome to try and implement
    that yourself.

    As for threads taking different times, and "hanging around", I don't see
    the problem. If each individual thread does resource cleanup - like
    connections - before calling await(), what is the issue with a thread
    doing basically nothing?

    AHS
     
    Arved Sandstrom, Jan 13, 2013
    #16
  17. On 12.01.2013 06:19, Knute Johnson wrote:
    > 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.


    I am surprised that this got just one mention so far. In absence of
    Executor (which has some issues, as has been mentioned) this is the most
    straightforward way to do it.

    // untested and from memory
    final Thread[] threads = new Thread[15];

    for ( int i = 0; i < threads.length; ++i ) {
    final Thread th = new Thread(...);
    th.start();
    threads = th;
    }

    for ( final Thread th : threads ) {
    th.join();
    }

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jan 13, 2013
    #17
  18. Roedy Green

    Roedy Green Guest

    On Sun, 13 Jan 2013 10:53:58 -0400, Arved Sandstrom
    <> wrote, quoted or indirectly quoted someone
    who said :

    >As for threads taking different times, and "hanging around", I don't see
    >the problem. If each individual thread does resource cleanup - like
    >connections - before calling await(), what is the issue with a thread
    >doing basically nothing?


    does not a thread have 1 MB + overhead just for existing?
    --
    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.
     
    Roedy Green, Jan 14, 2013
    #18
  19. Roedy Green

    Arne Vajhøj Guest

    On 1/13/2013 10:18 PM, Roedy Green wrote:
    > On Sun, 13 Jan 2013 10:53:58 -0400, Arved Sandstrom
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >> As for threads taking different times, and "hanging around", I don't see
    >> the problem. If each individual thread does resource cleanup - like
    >> connections - before calling await(), what is the issue with a thread
    >> doing basically nothing?

    >
    > does not a thread have 1 MB + overhead just for existing?


    Yes, no, it depends.

    It is implementation dependent so it can and do differ between
    different vendors and platforms.

    Some may provide an option for changing the default.

    I have once read that SUN/Oracle Java on Windows and Linux uses
    320 KB and 1024KB as default on 32 and 64 bit respectively.

    SUN/Oracle Java do provide the -Xss option to change the
    default.

    Arne
     
    Arne Vajhøj, Jan 14, 2013
    #19
  20. Roedy Green

    Arne Vajhøj Guest

    On 1/13/2013 2:39 AM, Kevin McMurtrie wrote:
    > In article <kcq2ap$m4o$>,
    > markspace <> wrote:
    >> 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!

    >
    > The Executor classes are horribly buggy. Pool size adjustment, idle
    > timeouts, and shutdown sequences do NOT work. Some of the features are
    > impossible to implement efficiently so Sun instead chose to implement
    > them incorrectly.
    >
    > For an Executor, it would be more correct to iterate through all the
    > Future or Callable objects returned and ask for their result.



    Can you be more specific about when shutdown and await does
    not work?

    Arne
     
    Arne Vajhøj, Jan 14, 2013
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    14,183
    Matt Humphrey
    Apr 22, 2005
  2. Behrang Dadsetan

    Waiting for processes to finish under Solaris

    Behrang Dadsetan, Jul 15, 2003, in forum: Python
    Replies:
    1
    Views:
    535
    Behrang Dadsetan
    Jul 15, 2003
  3. Martin DeMello
    Replies:
    1
    Views:
    203
    Farrel Lifson
    Nov 13, 2006
  4. Brett

    Waiting for a site to finish loading

    Brett, May 28, 2005, in forum: Javascript
    Replies:
    3
    Views:
    122
    Randy Webb
    May 28, 2005
  5. matej
    Replies:
    1
    Views:
    118
    Bjoern Hoehrmann
    Jun 25, 2008
Loading...

Share This Page