001package com.hammurapi.common.concurrent; 002 003 004/** 005 * This class increments counter when created and 006 * decrements when run() method exits. When counter reaches zero, this 007 * class invokes notifyAll() on the counter. The class shall be used for 008 * parallel execution of tasks when it is required to wait until all tasks 009 * complete. 010 * @author Pavel Vlasov 011 * 012 */ 013public class CountedRunnable implements Runnable { 014 015 /** 016 * Counter to use with CounterRunnable. 017 * @author Pavel Vlasov 018 */ 019 public static class Counter { 020 private int counter; 021 022 synchronized void inc() { 023 ++counter; 024 } 025 026 synchronized void dec() { 027 if (--counter <= 0) { 028 notifyAll(); 029 } 030 } 031 032 public synchronized void join() throws InterruptedException { 033 while (counter>0) { 034 wait(); 035 } 036 } 037 } 038 039 private Counter counter; 040 private Runnable master; 041 042 public CountedRunnable(Counter counter, Runnable master) { 043 this.counter = counter; 044 counter.inc(); 045 this.master = master; 046 } 047 048 final public void run() { 049 try { 050 master.run(); 051 } finally { 052 counter.dec(); 053 } 054 } 055 056}