Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * This file is a modified version of
   * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/AbstractExecutorService.java?revision=1.35
   * which contained the following notice:
   *
   * Written by Doug Lea with assistance from members of JCP JSR-166 Expert Group and released to the
   * public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/
   *
   * Rationale for copying:
  * Guava targets JDK5, whose AbstractExecutorService class lacks the newTaskFor protected
  * customization methods needed by MoreExecutors.listeningDecorator. This class is a copy of
  * AbstractExecutorService from the JSR166 CVS repository. It contains the desired methods.
  */
 
 package com.google.common.util.concurrent;
 
 import static com.google.common.util.concurrent.MoreExecutors.invokeAnyImpl;
 
 import java.util.List;
Implements ListeningExecutorService execution methods atop the abstract java.util.concurrent.Executor.execute(java.lang.Runnable) method. More concretely, the submit, invokeAny and invokeAll methods create ListenableFutureTask instances and pass them to java.util.concurrent.Executor.execute(java.lang.Runnable).

In addition to java.util.concurrent.Executor.execute(java.lang.Runnable), subclasses must implement all methods related to shutdown and termination.

Author(s):
Doug Lea
 
   @Override public ListenableFuture<?> submit(Runnable task) {
     ListenableFutureTask<Voidftask = ListenableFutureTask.create(tasknull);
     execute(ftask);
     return ftask;
   }
 
   @Override public <T> ListenableFuture<T> submit(Runnable task, T result) {
     ListenableFutureTask<T> ftask = ListenableFutureTask.create(taskresult);
     execute(ftask);
     return ftask;
   }
 
   @Override public <T> ListenableFuture<T> submit(Callable<T> task) {
     ListenableFutureTask<T> ftask = ListenableFutureTask.create(task);
     execute(ftask);
     return ftask;
   }
 
   @Override public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
       throws InterruptedExceptionExecutionException {
     try {
       return invokeAnyImpl(thistasksfalse, 0);
     } catch (TimeoutException cannotHappen) {
       throw new AssertionError();
     }
   }
 
   @Override public <T> T invokeAny(
       Collection<? extends Callable<T>> taskslong timeoutTimeUnit unit)
     return invokeAnyImpl(thistaskstrueunit.toNanos(timeout));
   }
 
   @Override public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
       throws InterruptedException {
     if (tasks == null) {
       throw new NullPointerException();
     }
     List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
     boolean done = false;
     try {
       for (Callable<T> t : tasks) {
         ListenableFutureTask<T> f = ListenableFutureTask.create(t);
         futures.add(f);
         execute(f);
       }
       for (Future<T> f : futures) {
         if (!f.isDone()) {
           try {
             f.get();
           } catch (CancellationException ignore) {
           } catch (ExecutionException ignore) {
           }
         }
       }
       done = true;
       return futures;
     } finally {
       if (!done) {
        for (Future<T> f : futures) {
          f.cancel(true);
        }
      }
    }
  }
  @Override public <T> List<Future<T>> invokeAll(
      Collection<? extends Callable<T>> taskslong timeoutTimeUnit unit)
      throws InterruptedException {
    if (tasks == null || unit == null) {
      throw new NullPointerException();
    }
    long nanos = unit.toNanos(timeout);
    List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
    boolean done = false;
    try {
      for (Callable<T> t : tasks) {
        futures.add(ListenableFutureTask.create(t));
      }
      long lastTime = System.nanoTime();
      // Interleave time checks and calls to execute in case
      // executor doesn't have any/much parallelism.
      Iterator<Future<T>> it = futures.iterator();
      while (it.hasNext()) {
        execute((Runnable) (it.next()));
        long now = System.nanoTime();
        nanos -= now - lastTime;
        lastTime = now;
        if (nanos <= 0) {
          return futures;
        }
      }
      for (Future<T> f : futures) {
        if (!f.isDone()) {
          if (nanos <= 0) {
            return futures;
          }
          try {
            f.get(nanos.);
          } catch (CancellationException ignore) {
          } catch (ExecutionException ignore) {
          } catch (TimeoutException toe) {
            return futures;
          }
          long now = System.nanoTime();
          nanos -= now - lastTime;
          lastTime = now;
        }
      }
      done = true;
      return futures;
    } finally {
      if (!done) {
        for (Future<T> f : futures) {
          f.cancel(true);
        }
      }
    }
  }
New to GrepCode? Check out our FAQ X