Class SyncTaskExecutor
- All Implemented Interfaces:
Executor,ContextAwareTaskExecutor,ContextAwareTaskExecutorService,MonitorableTaskExecutor,MonitorableTaskExecutorService,TaskExecutor,TaskExecutorService
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 efficientTaskExecutorinstance which executes tasks synchronously on the calling thread. -
getDefaultInstance()to use as sensible default values when anTaskExecutorServiceinstance is needed.
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.-
Field Summary
Fields inherited from class org.jtrim2.executor.DelegatedTaskExecutorService
wrappedExecutor -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new executor which executes tasks synchronously on the calling thread. -
Method Summary
Modifier and TypeMethodDescriptionstatic TaskExecutorServiceReturns anTaskExecutorServicewhich executes tasks synchronously on the calling thread and cannot be shutted down.longReturns the approximate number of tasks currently being executed.longReturns the approximate number of tasks currently queued to this executor.static TaskExecutorReturns a plain and efficientTaskExecutorwhich executes tasks synchronously on the calling thread.booleanReturnstrueif the calling code is executing in the context of this executor.Methods inherited from class org.jtrim2.executor.DelegatedTaskExecutorService
addTerminateListener, awaitTermination, execute, execute, executeFunction, executeStaged, isShutdown, isTerminated, shutdown, shutdownAndCancel, toString, tryAwaitTerminationMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jtrim2.executor.TaskExecutor
execute, execute, executeFunction, executeStagedMethods inherited from interface org.jtrim2.executor.TaskExecutorService
addTerminateListener, awaitTermination, isShutdown, isTerminated, shutdown, shutdownAndCancel, tryAwaitTermination
-
Constructor Details
-
SyncTaskExecutor
public SyncTaskExecutor()Creates a new executor which executes tasks synchronously on the calling thread.
-
-
Method Details
-
getSimpleExecutor
Returns a plain and efficientTaskExecutorwhich executes tasks synchronously on the calling thread. This method always returns the same executor instance and is considerably more efficient than a full-fledgedSyncTaskExecutorimplementation (or the one returned bygetDefaultInstance().- Returns:
- a plain and efficient
TaskExecutorwhich executes tasks synchronously on the calling thread. This method never returnsnull.
-
getDefaultInstance
Returns anTaskExecutorServicewhich executes tasks synchronously on the calling thread and cannot be shutted down. Attempting to shutdown the returnedTaskExecutorServicewill result in an uncheckedUnsupportedOperationExceptionto be thrown.This method always returns the same
TaskExecutorServiceinstance 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 thisTaskExecutorServiceinstance is only intended to be used as a sensible default value.- Returns:
- an
TaskExecutorServicewhich executes tasks synchronously on the calling thread and cannot be shutted down. This method never returnsnulland 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:
getNumberOfQueuedTasksin interfaceMonitorableTaskExecutor- 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:
getNumberOfExecutingTasksin interfaceMonitorableTaskExecutor- 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()Returnstrueif 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:
isExecutingInThisin interfaceContextAwareTaskExecutor- Returns:
trueif the calling code is executing in the context of this executor,falseotherwise
-