Class SameThreadExecutorService

  • All Implemented Interfaces:
    java.util.concurrent.Executor, java.util.concurrent.ExecutorService

    class SameThreadExecutorService
    extends java.util.concurrent.AbstractExecutorService
    Creates an executor service that runs each task in the thread that invokes execute/submit, as in ThreadPoolExecutor.CallerRunsPolicy This applies both to individually submitted tasks and to collections of tasks submitted via invokeAll or invokeAny. In the latter case, tasks will run serially on the calling thread. Tasks are run to completion before a Future is returned to the caller (unless the executor has been shutdown).

    Although all tasks are immediately executed in the thread that submitted the task, this ExecutorService imposes a small locking overhead on each task submission in order to implement shutdown and termination behavior.

    The implementation deviates from the ExecutorService specification with regards to the shutdownNow method. First, "best-effort" with regards to canceling running tasks is implemented as "no-effort". No interrupts or other attempts are made to stop threads executing tasks. Second, the returned list will always be empty, as any submitted task is considered to have started execution. This applies also to tasks given to invokeAll or invokeAny which are pending serial execution, even the subset of the tasks that have not yet started execution. It is unclear from the ExecutorService specification if these should be included, and it's much easier to implement the interpretation that they not be. Finally, a call to shutdown or shutdownNow may result in concurrent calls to invokeAll/invokeAny throwing RejectedExecutionException, although a subset of the tasks may already have been executed.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.locks.Lock lock
      Lock used whenever accessing the state variables (runningTasks, shutdown, terminationCondition) of the executor
      private int runningTasks  
      private boolean shutdown  
      private java.util.concurrent.locks.Condition termination
      Signaled after the executor is shutdown and running tasks are done
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean awaitTermination​(long timeout, java.util.concurrent.TimeUnit unit)  
      private void endTask()
      Decrements the running task count.
      void execute​(java.lang.Runnable command)  
      boolean isShutdown()  
      boolean isTerminated()  
      void shutdown()  
      java.util.List<java.lang.Runnable> shutdownNow()  
      private void startTask()
      Checks if the executor has been shut down and increments the running task count.
      • Methods inherited from class java.util.concurrent.AbstractExecutorService

        invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • lock

        private final java.util.concurrent.locks.Lock lock
        Lock used whenever accessing the state variables (runningTasks, shutdown, terminationCondition) of the executor
      • termination

        private final java.util.concurrent.locks.Condition termination
        Signaled after the executor is shutdown and running tasks are done
      • runningTasks

        private int runningTasks
      • shutdown

        private boolean shutdown
    • Constructor Detail

      • SameThreadExecutorService

        SameThreadExecutorService()
    • Method Detail

      • execute

        public void execute​(java.lang.Runnable command)
      • isShutdown

        public boolean isShutdown()
      • shutdown

        public void shutdown()
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
      • isTerminated

        public boolean isTerminated()
      • awaitTermination

        public boolean awaitTermination​(long timeout,
                                        java.util.concurrent.TimeUnit unit)
                                 throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • startTask

        private void startTask()
        Checks if the executor has been shut down and increments the running task count.
        Throws:
        java.util.concurrent.RejectedExecutionException - if the executor has been previously shutdown
      • endTask

        private void endTask()
        Decrements the running task count.