01: package org.apache.mina.common;
02:
03: import java.util.concurrent.atomic.AtomicInteger;
04:
05: /**
06: * An {@link IoFuture} of {@link IoFuture}s. It is useful when you want to
07: * get notified when all {@link IoFuture}s are complete. It is not recommended
08: * to use {@link CompositeIoFuture} if you just want to wait for all futures.
09: * In that case, please use {@link IoUtil#await(Iterable)} instead
10: * for better performance.
11: *
12: * @author The Apache MINA Project (dev@mina.apache.org)
13: * @version $Rev: 589474 $, $Date: 2007-10-28 21:03:14 -0600 (Sun, 28 Oct 2007) $
14: *
15: * @param <E> the type of the child futures.
16: */
17: public class CompositeIoFuture<E extends IoFuture> extends
18: DefaultIoFuture {
19:
20: private final NotifyingListener listener = new NotifyingListener();
21: private final AtomicInteger unnotified = new AtomicInteger();
22: private volatile boolean constructionFinished;
23:
24: public CompositeIoFuture(Iterable<E> children) {
25: super (null);
26:
27: for (E f : children) {
28: f.addListener(listener);
29: unnotified.incrementAndGet();
30: }
31:
32: constructionFinished = true;
33: if (unnotified.get() == 0) {
34: setValue(true);
35: }
36: }
37:
38: private class NotifyingListener implements
39: IoFutureListener<IoFuture> {
40: public void operationComplete(IoFuture future) {
41: if (unnotified.decrementAndGet() == 0
42: && constructionFinished) {
43: setValue(true);
44: }
45: }
46: }
47: }
|