Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.infinispan.commons.util.concurrent;
 
 import java.util.List;
CompositeNotifyingFuture aggregates several NotifyingFuture and completes when all of them complete.

Author(s):
gustavonalle
Since:
7.1
public final class CompositeNotifyingFuture<T> extends NotifyingFutureImpl<List<T>> {
   private final CountDownLatch remaining;
   private final List<NotifyingFuture<T>> futures;
   private volatile boolean cancelled = false;
   private List<T> results = new ArrayList<>();
   public CompositeNotifyingFuture(List<NotifyingFuture<T>> futures) {
      this. = futures;
      this. = new CountDownLatch(futures.size());
      InternalListener internalListener = new InternalListener();
      for (NotifyingFuture<T> future : futures) {
         future.attachListener(internalListener);
      }
   }
   @Override
   public boolean isDone() {
      return .getCount() == 0;
   }
   @Override
   public boolean isCancelled() {
      return ;
   }
   @Override
   public boolean cancel(boolean mayInterruptIfRunning) {
      this. = true;
      boolean wasCancelled = true;
      for (NotifyingFuture<?> future : ) {
         wasCancelled &= future.cancel(mayInterruptIfRunning);
      }
      return wasCancelled;
   }
   @Override
   public List<T> get(long timeoutTimeUnit unitthrows InterruptedExceptionExecutionExceptionTimeoutException {
      if (unit == nullthrow new IllegalArgumentException("provided unit is null");
      if (throw new CancellationException();
      if (!.await(timeoutunit)) throw new TimeoutException();
      return super.get();
   }
   @Override
   public List<T> get() throws InterruptedExceptionExecutionException {
      if (throw new CancellationException();
      .await();
      return super.get();
   }
   final class InternalListener implements FutureListener<T> {
      @Override
      public void futureDone(Future<T> future) {
         synchronized (this) {
            Throwable error = null;
            try {
               .add(future.get());
            } catch (Throwable e) {
               error = e;
            }
            .countDown();
            if (.getCount() == 0) {
               if (error != null) {
                  if (error instanceof ExecutionException) {
                     notifyException(((ExecutionException)error).getCause());
                  } else {
                     notifyException(error);
                  }
               } else {
                  notifyDone();
               }
               setFuture(new NoOpFuture<>());
            }
         }
      }
   }
New to GrepCode? Check out our FAQ X