Class SyncTaskExecutor
- All Implemented Interfaces:
Serializable, Executor, TaskExecutor
TaskExecutor implementation that executes each task synchronously
in the calling thread. This can be used for testing purposes but also for
bounded execution in a Virtual Threads setup, relying on concurrency throttling
as inherited from the base class: see ConcurrencyThrottleSupport.setConcurrencyLimit(int) (as of 7.0).
Execution in the calling thread does have the advantage of participating
in its thread context, for example the thread context class loader or the
thread's current transaction association. That said, in many cases,
asynchronous execution will be preferable: choose an asynchronous
TaskExecutor instead for such scenarios.
- Since:
- 2.0
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
Fields inherited from class ConcurrencyThrottleSupport
logger, NO_CONCURRENCY, UNBOUNDED_CONCURRENCY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidExecute the giventasksynchronously, through direct invocation of itsrun()method.execute(TaskCallback<V, E> task) Execute the giventasksynchronously, through direct invocation of itscall()method.protected voidonAccessRejected(String msg) Triggered when access has been rejected due to the concurrency policy or due to interruption.protected voidTriggered byConcurrencyThrottleSupport.beforeAccess()when the concurrency limit has been reached.voidsetRejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) Specify whether to reject tasks when the concurrency limit has been reached, throwingTaskRejectedException(which extends the commonRejectedExecutionException) on any further execution attempts.Methods inherited from class ConcurrencyThrottleSupport
afterAccess, beforeAccess, getConcurrencyLimit, isThrottleActive, setConcurrencyLimit
-
Constructor Details
-
SyncTaskExecutor
public SyncTaskExecutor()
-
-
Method Details
-
setRejectTasksWhenLimitReached
public void setRejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) Specify whether to reject tasks when the concurrency limit has been reached, throwingTaskRejectedException(which extends the commonRejectedExecutionException) on any further execution attempts.The default is
false, blocking the caller until the submission can be accepted. Switch this totruefor immediate rejection instead.- Since:
- 7.0.3
- See Also:
-
execute
Execute the giventasksynchronously, through direct invocation of itsrun()method.This can be used with a
concurrency limit, analogous to a concurrency-boundedSimpleAsyncTaskExecutorsetup. Also, the provided task may apply a retry policy viaRetryTask.- Specified by:
executein interfaceExecutor- Specified by:
executein interfaceTaskExecutor- Parameters:
task- theRunnableto execute (nevernull)- Throws:
RuntimeException- if propagated from the givenRunnable- See Also:
-
execute
Execute the giventasksynchronously, through direct invocation of itscall()method.This can be used with a
concurrency limit, analogous to a concurrency-boundedSimpleAsyncTaskExecutorsetup. Also, the provided task may apply a retry policy viaRetryTask.- Type Parameters:
V- the returned value type, if anyE- the exception propagated, if any- Throws:
E- if propagated from the givenTaskCallback- Since:
- 7.0
- See Also:
-
onLimitReached
protected void onLimitReached()Description copied from class:ConcurrencyThrottleSupportTriggered byConcurrencyThrottleSupport.beforeAccess()when the concurrency limit has been reached. The default implementation blocks until the concurrency count allows for entering.- Overrides:
onLimitReachedin classConcurrencyThrottleSupport
-
onAccessRejected
Description copied from class:ConcurrencyThrottleSupportTriggered when access has been rejected due to the concurrency policy or due to interruption.Implementations will typically throw a corresponding exception. When returning normally, regular access will still be attempted.
The default implementation throws an
IllegalStateException.- Overrides:
onAccessRejectedin classConcurrencyThrottleSupport- Parameters:
msg- the rejection message (common exception messages are designed so that they can be appended with an identifier separated by a space in custom subclasses)
-