/** * Barrier used to put a thread to sleep until an action has happened a * given number of times. * * @author Michael Parker */ public class CountingBarrier { protected int count; protected int release; public CountingBarrier() { this(0); } public CountingBarrier(int new_release) { set(new_release); } public synchronized void set(int new_release) { count = 0; release = new_release; } public synchronized void await() { while (count < release) { try { wait(); } catch (InterruptedException e) { // interrupted, so just return return; } } } public synchronized void touch() { if (count >= release) { return; } else if (++count == release) { notifyAll(); } } public synchronized String toString() { StringBuffer sbuf = new StringBuffer(1024); sbuf.append("count = ").append(count); sbuf.append(", release = ").append(release); return sbuf.toString(); } }