Class SyncTaskExecutor

All Implemented Interfaces:
Executor, ContextAwareTaskExecutor, ContextAwareTaskExecutorService, MonitorableTaskExecutor, MonitorableTaskExecutorService, TaskExecutor, TaskExecutorService

public final class SyncTaskExecutor extends DelegatedTaskExecutorService implements MonitorableTaskExecutorService
Defines an executor which executes tasks synchronously on the calling thread which submits them (by the execute or one of the submit methods). Therefore whenever such execute or submit method returns, the submitted task is already executed or never will be (due to the executor being shut down).

There are two special instances which can be accessed through static methods:

  • getSimpleExecutor() for a simple and efficient TaskExecutor instance which executes tasks synchronously on the calling thread.
  • getDefaultInstance() to use as sensible default values when an TaskExecutorService instance is needed.
Note that unlike general TaskExecutorService instances, instances of this class does not need to be shut down (but it is possible to do so).

Thread safety

The methods of this class are safe to use by multiple threads concurrently.

Synchronization transparency

The methods of this class are not synchronization transparent.
  • Constructor Details

    • SyncTaskExecutor

      public SyncTaskExecutor()
      Creates a new executor which executes tasks synchronously on the calling thread.
  • Method Details

    • getSimpleExecutor

      public static TaskExecutor getSimpleExecutor()
      Returns a plain and efficient TaskExecutor which executes tasks synchronously on the calling thread. This method always returns the same executor instance and is considerably more efficient than a full-fledged SyncTaskExecutor implementation (or the one returned by getDefaultInstance().
      Returns:
      a plain and efficient TaskExecutor which executes tasks synchronously on the calling thread. This method never returns null.
    • getDefaultInstance

      public static TaskExecutorService getDefaultInstance()
      Returns an TaskExecutorService which executes tasks synchronously on the calling thread and cannot be shutted down. Attempting to shutdown the returned TaskExecutorService will result in an unchecked UnsupportedOperationException to be thrown.

      This method always returns the same TaskExecutorService instance and note that sharing the return value across multiple threads can cause some synchronization overhead. So it is more efficient to create a new instance when this is an issue. Therefore this TaskExecutorService instance is only intended to be used as a sensible default value.

      Returns:
      an TaskExecutorService which executes tasks synchronously on the calling thread and cannot be shutted down. This method never returns null and always returns the same instance.
    • getNumberOfQueuedTasks

      public long getNumberOfQueuedTasks()
      Returns the approximate number of tasks currently queued to this executor. The queued tasks are not currently executing but are scheduled to be executed in the future.

      Note that the value returned by this method should be considered unreliable and cannot be used for synchronization purposes.

      Implementation note: This method always returns zero, since this executor never queues tasks.

      Specified by:
      getNumberOfQueuedTasks in interface MonitorableTaskExecutor
      Returns:
      the approximate number of tasks currently queued to this executor. This method always returns a value greater than or equal to zero.
    • getNumberOfExecutingTasks

      public long getNumberOfExecutingTasks()
      Returns the approximate number of tasks currently being executed.

      Note that the value returned by this method should be considered unreliable and cannot be used for synchronization purposes.

      Specified by:
      getNumberOfExecutingTasks in interface MonitorableTaskExecutor
      Returns:
      the approximate number of tasks currently being executed. This method always returns a value greater than or equal to zero.
    • isExecutingInThis

      public boolean isExecutingInThis()
      Returns true if the calling code is executing in the context of this executor. That is, it is executed by a task submitted to this executor.

      This method can be used to check that a method call is executing in the context it was designed for.

      Specified by:
      isExecutingInThis in interface ContextAwareTaskExecutor
      Returns:
      true if the calling code is executing in the context of this executor, false otherwise