Class LazyExecutorService

java.lang.Object
com.google.cloud.hadoop.util.LazyExecutorService
All Implemented Interfaces:
Executor, ExecutorService

@GwtIncompatible public final class LazyExecutorService extends Object implements ExecutorService
Defers execution to the time that a method that expresses interest in the result (get or isDone) is called on the Future. Execution is performed by a backing ExecutorService.

In essence, a returned Future represents a "canned" method call and once the call has been performed, the Future returns the cached result.

Both this class and the returned Future are thread-safe.

Author:
tobe@google.com (Torbjorn Gannholm), jlevy@google.com (Jared Levy), cpovirk@google.com (Chris Povirk)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an instance using a MoreExecutors.newDirectExecutorService() for the backing service.
    Creates an instance using the given ExecutorService as the backing service.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    awaitTermination(long timeout, TimeUnit unit)
     
    void
    execute(Runnable command)
    Always throws a RejectedExecutionException because using this method does not make sense from either a lazy execution perspective or a cached result perspective.
    <T> List<Future<T>>
    invokeAll(Collection<? extends Callable<T>> tasks)
    ExecutorService requires that this method should not return until all tasks are completed, which precludes lazy execution.
    <T> List<Future<T>>
    invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
    ExecutorService requires that this method should not return until all tasks are completed or the timeout expires, which precludes lazy execution.
    <T> T
    invokeAny(Collection<? extends Callable<T>> tasks)
    Always throws a RejectedExecutionException because using this method does not make sense from either a lazy execution perspective or a cached result perspective.
    <T> T
    invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
    Always throws a RejectedExecutionException because using this method does not make sense from either a lazy execution perspective or a cached result perspective.
    boolean
     
    boolean
     
    void
    Shuts this service down, but leaves the backing service untouched.
    Trying to interpret the assumptions about the contract of this method in the light of this implementation, it seems most reasonable to take the view that all tasks are running, even if the processing has not actually started.
    submit(Runnable command)
     
    <T> Future<T>
    submit(Runnable task, T result)
     
    <T> Future<T>
    submit(Callable<T> task)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • LazyExecutorService

      public LazyExecutorService()
      Creates an instance using a MoreExecutors.newDirectExecutorService() for the backing service.
    • LazyExecutorService

      public LazyExecutorService(ExecutorService backingService)
      Creates an instance using the given ExecutorService as the backing service.

      The backing service will only be used to execute tasks and it may be shared by several instances or used for other purposes. Shutdowns of this instance will not shut down the backing service.

      If you shut down the backing service, this instance will be shut down automatically and all tasks submitted to this instance that have not yet been submitted to the backing service will be considered cancelled.

  • Method Details

    • shutdown

      public void shutdown()
      Shuts this service down, but leaves the backing service untouched.
      Specified by:
      shutdown in interface ExecutorService
    • shutdownNow

      @CanIgnoreReturnValue public List<Runnable> shutdownNow()
      Trying to interpret the assumptions about the contract of this method in the light of this implementation, it seems most reasonable to take the view that all tasks are running, even if the processing has not actually started. Therefore, unfinished tasks will be cancelled and an empty list will be returned.
      Specified by:
      shutdownNow in interface ExecutorService
    • isShutdown

      public boolean isShutdown()
      Specified by:
      isShutdown in interface ExecutorService
    • isTerminated

      public boolean isTerminated()
      Specified by:
      isTerminated in interface ExecutorService
    • awaitTermination

      public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
      Specified by:
      awaitTermination in interface ExecutorService
      Throws:
      InterruptedException
    • submit

      public <T> Future<T> submit(Callable<T> task)
      Specified by:
      submit in interface ExecutorService
    • submit

      public <T> Future<T> submit(Runnable task, T result)
      Specified by:
      submit in interface ExecutorService
    • submit

      public Future<?> submit(Runnable command)
      Specified by:
      submit in interface ExecutorService
    • invokeAll

      @CanIgnoreReturnValue public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
      ExecutorService requires that this method should not return until all tasks are completed, which precludes lazy execution. Tasks are run in parallel, as far as the backing service allows.

      This method makes sense from a cached result perspective but not from a lazy execution perspective.

      Specified by:
      invokeAll in interface ExecutorService
      Throws:
      InterruptedException
    • invokeAll

      @CanIgnoreReturnValue public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
      ExecutorService requires that this method should not return until all tasks are completed or the timeout expires, which precludes lazy execution. Tasks are run in parallel, as far as the backing service allows. Timeout is done as a best-effort in case of the default same thread executor.

      This method makes sense from a cached result perspective but not from a lazy execution perspective.

      Specified by:
      invokeAll in interface ExecutorService
      Throws:
      InterruptedException
    • invokeAny

      public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
      Always throws a RejectedExecutionException because using this method does not make sense from either a lazy execution perspective or a cached result perspective.
      Specified by:
      invokeAny in interface ExecutorService
    • invokeAny

      public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
      Always throws a RejectedExecutionException because using this method does not make sense from either a lazy execution perspective or a cached result perspective.
      Specified by:
      invokeAny in interface ExecutorService
    • execute

      public void execute(Runnable command)
      Always throws a RejectedExecutionException because using this method does not make sense from either a lazy execution perspective or a cached result perspective.
      Specified by:
      execute in interface Executor