[concurrency-interest] CyclicBarrier / Count Down Latch

Gregg Wonderly gregg at cytetech.com
Mon Apr 20 15:41:00 EDT 2009


In many of my applications, I have some startup synchronization that needs to 
occur before the system can proceed to an active state.  I always have something 
like the following that I use because it makes the system independent of new 
threads of changes in threading that I make over time.

public class Progress {
	private boolean ready;
	private final Object lock = new Object();
	public void await() {
		synchronized( lock ) {
			while( !ready )
				lock.wait();
		}
	}
	public void ready() {
		synchronized( lock ) {
			ready = true;
			lock.notifyAll();
		}
	}
}

I can then just create an instance and use it for for whatever synchronization I 
need to wait for.  Threads of execution can then be blocked until everything is
ready to go and the appropriate ready() call is made.

Gregg Wonderly

javamann at cox.net wrote:
> Howdy,
>   I have an application that generates an unknown amount of threads at startup (via JMX MLet) and I was looking for a way to block all the threads at a certain point and then 'release' them to continue. I looked at the CyclicBarrier and CountDownLatch but they require you know the number of threads ahead of time. Currently I am just using a 'global' Condition for them to acquire and then Signal them all to continue. This seems like a hack and I am hoping there is a better way to accomplish this. Any Ideas?
> 
> Thanks
> 
> -Pete
> 
> --
> 
> 1. If a man is standing in the middle of the forest talking, and there is no woman around to hear him, is he still wrong?
> 
> 2. Behind every great woman... Is a man checking out her ass
> 
> 3. I am not a member of any organized political party. I am a Democrat.*
> 
> 4. Diplomacy is the art of saying "Nice doggie" until you can find a rock.*
> 
> 5. A process is what you need when all your good people have left.
> 
> 
> *Will Rogers
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> 
> 



More information about the Concurrency-interest mailing list