Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #21408

Re: Threads, waiting for last one to finish

From Kevin McMurtrie <mcmurtrie@pixelmemory.us>
Newsgroups comp.lang.java.programmer
Subject Re: Threads, waiting for last one to finish
References <oe21f8d1h1d357bljli7009crmaf3kmag9@4ax.com> <kcq2ap$m4o$1@dont-email.me> <50f264aa$0$80119$742ec2ed@news.sonic.net> <50f37d14$0$293$14726298@news.sunsite.dk>
Date 2013-01-14 22:29 -0800
Message-ID <50f4f730$0$80137$742ec2ed@news.sonic.net> (permalink)
Organization Sonic.Net

Show all headers | View raw


In article <50f37d14$0$293$14726298@news.sunsite.dk>,
 Arne Vajhøj <arne@vajhoej.dk> wrote:

> On 1/13/2013 2:39 AM, Kevin McMurtrie wrote:
> > In article <kcq2ap$m4o$1@dont-email.me>,
> >   markspace <markspace@nospam.nospam> 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

There are race conditions in determining the predicted thread states.  
There's simply no way to check the work queue plus all of the states of 
all of the threads in an atomic manner without locking or using a master 
thread.  Sun's implementation has almost no locking and no master thread 
so it makes mistakes while threads are transitioning states.  The 
variable sized thread pool is the worst (glitches maybe 10% of the 
time!) but the others have rare glitches too.  These glitches are fixed 
when a new task is queued so a steady stream of tasks appears to work 
correctly.  What happened with the very last task is a gamble.  That's 
why you shouldn't use shutdown() to wait for completion.

I'm not going to walk through the code here.  It's awful stuff that's 
difficult to follow and it changes a little with each JVM release.  You 
can step through it in a debugger and see that there's some wishful 
thinking in there.  Careful placement of breakpoints can produce a fault 
that doesn't recover when all breakpoints are removed.
-- 
I will not see posts from Google because I must filter them as spam

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-11 13:56 -0800
  Re: Threads, waiting for last one to finish markspace <markspace@nospam.nospam> - 2013-01-11 14:05 -0800
    Re: Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-11 14:14 -0800
      Re: Threads, waiting for last one to finish markspace <markspace@nospam.nospam> - 2013-01-11 15:11 -0800
        Re: Threads, waiting for last one to finish Arne Vajhøj <arne@vajhoej.dk> - 2013-01-11 20:02 -0500
        Re: Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-12 01:51 -0800
        Re: Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-12 02:20 -0800
          Re: Threads, waiting for last one to finish Arne Vajhøj <arne@vajhoej.dk> - 2013-01-12 09:08 -0500
        Re: Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-12 10:36 -0800
          Re: Threads, waiting for last one to finish Arne Vajhøj <arne@vajhoej.dk> - 2013-01-12 16:17 -0500
    Re: Threads, waiting for last one to finish Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-12 23:39 -0800
      Re: Threads, waiting for last one to finish Arne Vajhøj <arne@vajhoej.dk> - 2013-01-13 22:35 -0500
        Re: Threads, waiting for last one to finish Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-14 09:12 -0800
        Re: Threads, waiting for last one to finish Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-14 22:29 -0800
          Re: Threads, waiting for last one to finish markspace <markspace@nospam.nospam> - 2013-01-15 08:19 -0800
            Re: Threads, waiting for last one to finish Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-16 01:28 -0800
              Re: Threads, waiting for last one to finish Robert Klemme <shortcutter@googlemail.com> - 2013-01-20 15:21 +0100
            Re: Threads, waiting for last one to finish Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-18 21:58 -0800
      Re: Threads, waiting for last one to finish Robert Klemme <shortcutter@googlemail.com> - 2013-01-26 17:20 +0100
  Re: Threads, waiting for last one to finish Knute Johnson <nospam@knutejohnson.com> - 2013-01-11 21:19 -0800
    Re: Threads, waiting for last one to finish Robert Klemme <shortcutter@googlemail.com> - 2013-01-13 19:38 +0100
      Re: Threads, waiting for last one to finish Knute Johnson <nospam@knutejohnson.com> - 2013-01-14 08:58 -0800
      Re: Threads, waiting for last one to finish Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-15 05:05 -0400
  Re: Threads, waiting for last one to finish Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-12 08:42 -0400
    Re: Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-12 23:56 -0800
      Re: Threads, waiting for last one to finish Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-13 10:53 -0400
        Re: Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-13 19:18 -0800
          Re: Threads, waiting for last one to finish Arne Vajhøj <arne@vajhoej.dk> - 2013-01-13 22:26 -0500
            Re: Threads, waiting for last one to finish Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-14 05:51 -0400
              Re: Threads, waiting for last one to finish Arne Vajhøj <arne@vajhoej.dk> - 2013-01-14 19:02 -0500
        Re: Threads, waiting for last one to finish Roedy Green <see_website@mindprod.com.invalid> - 2013-01-13 19:43 -0800
  Re: Threads, waiting for last one to finish Sebastian <news@seyweiler.dyndns.org> - 2013-01-13 15:19 +0100

csiph-web