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