Class JBossExecutors
java.lang.Object
org.jboss.threads.JBossExecutors
JBoss thread- and executor-related utility and factory methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic RejectedExecutionHandlerGet the abort policy for aThreadPoolExecutor.static BlockingExecutorDeprecated.static BlockingExecutorDeprecated.static BlockingExecutorDeprecated.static RejectedExecutionHandlerGet the caller-runs policy for aThreadPoolExecutor.static RunnableclassLoaderPreservingTask(Runnable delegate) Create a task that delegates to the given task, preserving the context classloader which was in effect when this method was invoked.static DirectExecutorcleanupExecutor(DirectExecutor delegate, Runnable cleaner) Create an executor which runs the given cleanup task after running its given task.static RunnablecompositeTask(Runnable... runnables) Create a task that is a composite of several other tasks.static RunnablecompositeTask(Collection<Runnable> runnables) Create a task that is a composite of several other tasks.static DirectExecutorcontextClassLoaderExecutor(DirectExecutor delegate, ClassLoader taskClassLoader) Create a direct executor which runs tasks with the given context class loader.static RunnableGet aRunnablewhich, when executed, clears the thread context class loader (if the caller has sufficient privileges).static DependencyTaskBuilderdependencyTaskBuilder(Executor executor, Runnable task) Create a builder for a dependent task.static BlockingExecutordirectBlockingExecutor(DirectExecutor delegate) Deprecated.static DirectExecutorGet the direct executor.static DirectExecutorServiceGet the direct executor service.static DirectExecutorGet the discarding executor.static DirectExecutorServiceGet the discarding executor service.static RejectedExecutionHandlerGet the discard-oldest policy for aThreadPoolExecutor.static RejectedExecutionHandlerGet the discard policy for aThreadPoolExecutor.static DirectExecutorexceptionLoggingExecutor(DirectExecutor delegate) Create a direct executor which consumes and logs errors that are thrown to the default thread error category"org.jboss.threads.errors".static DirectExecutorexceptionLoggingExecutor(DirectExecutor delegate, org.jboss.logging.Logger log) Create a direct executor which consumes and logs errors that are thrown.static DirectExecutorexceptionLoggingExecutor(DirectExecutor delegate, org.jboss.logging.Logger log, org.jboss.logging.Logger.Level level) Create a direct executor which consumes and logs errors that are thrown.static voidexecuteUninterruptibly(Executor executor, Runnable task) Execute a task uninterruptibly.static Executorexecutor(WrappingExecutor delegate, DirectExecutor taskWrapper) An executor which delegates to a wrapping executor, wrapping each task in a task wrapper.static RunnableexecutorTask(DirectExecutor executor, Runnable task) Get a task that runs the given task through the given direct executor.static RejectedExecutionHandlerhandoffPolicy(Executor target) Get a handoff policy for aThreadPoolExecutor.static DirectExecutorinitializingExecutor(DirectExecutor delegate, Runnable initializer) Create an executor which runs the given initialization task before running its given task.Get an uncaught exception handler which logs to the default error logger.loggingExceptionHandler(String categoryName) Get an uncaught exception handler which logs to the given logger.loggingExceptionHandler(org.jboss.logging.Logger log) Get an uncaught exception handler which logs to the given logger.static <A> DirectExecutornotifyingDirectExecutor(DirectExecutor delegate, TaskNotifier<Runnable, ? super A> notifier, A attachment) Get a notifying direct executor.notifyingRunnable(R task, TaskNotifier<? super R, ? super A> notifier, A attachment) Get a notifying runnable wrapper for a task.static RunnableGet the null runnable which does nothing.static DirectExecutorprivilegedExecutor(DirectExecutor delegate) Create a direct executor which runs with the privileges given by the current access control context.static BlockingExecutorDeprecated.static BlockingExecutorServiceDeprecated.static DirectExecutorServiceWrap a direct executor with anDirectExecutorServiceinstance which supports all the features ofExecutorServiceexcept for shutting down the executor.static ExecutorServiceprotectedExecutorService(Executor target) Wrap an executor with anExecutorServiceinstance which supports all the features ofExecutorServiceexcept for shutting down the executor.static ScheduledExecutorServiceWrap a scheduled executor with aScheduledExecutorServiceinstance which supports all the features ofScheduledExecutorServiceexcept for shutting down the executor.static DirectExecutorGet the rejecting executor.static DirectExecutorrejectingExecutor(String message) Get a rejecting executor.static DirectExecutorServiceGet the rejecting executor service.static DirectExecutorServicerejectingExecutorService(String message) Get the rejecting executor service.static DirectExecutorresettingExecutor(DirectExecutor delegate) Create a direct executor which delegates tasks to the given executor, and then clears all thread-local data after each task completes (regardless of outcome).static ThreadFactoryresettingThreadFactory(ThreadFactory delegate) Create a thread factory which resets all thread-local storage and delegates to the given thread factory.static <R extends Runnable, A>
voidrun(R task, DirectExecutor directExecutor, TaskNotifier<? super R, ? super A> notifier, A attachment) Run a task through the given direct executor, invoking the given notifier with the given attachment.static <R extends Runnable, A>
voidrun(R task, TaskNotifier<? super R, ? super A> notifier, A attachment) Run a task, invoking the given notifier with the given attachment.static BlockingExecutorthreadFactoryExecutor(ThreadFactory factory) Deprecated.static BlockingExecutorthreadFactoryExecutor(ThreadFactory factory, int maxThreads) Deprecated.UseEnhancedQueueExecutorinstead.static BlockingExecutorthreadFactoryExecutor(ThreadFactory factory, int maxThreads, boolean blocking) Deprecated.UseEnhancedQueueExecutorinstead.static BlockingExecutorthreadFactoryExecutor(ThreadFactory factory, int maxThreads, boolean blocking, DirectExecutor taskExecutor) Deprecated.UseEnhancedQueueExecutorinstead.static DirectExecutorthreadFormattedNameExecutor(DirectExecutor delegate, String newName) Create a direct executor which changes the thread name for the duration of a task using a formatted name.static RunnableGet aRunnablewhich, when executed, clears the thread-local storage of the calling thread.static DirectExecutorthreadNameExecutor(DirectExecutor delegate, String newName) Create a direct executor which changes the thread name for the duration of a task.static DirectExecutorthreadNameNotateExecutor(DirectExecutor delegate, String notation) Create a direct executor which adds a note to the thread name for the duration of a task.static ExecutoruninterruptibleExecutor(Executor delegate) Get an executor which executes tasks uninterruptibly in the event of blocking.static WrappingExecutorwrappingExecutor(Executor delegate) Create a wrapping executor for a delegate executor which creates anexecutorTask(DirectExecutor, Runnable)for each task.static ExecutorwrappingExecutor(DirectExecutor taskWrapper, Executor delegate) An executor which delegates to another executor, wrapping each task in a task wrapper.static ThreadFactorywrappingThreadFactory(DirectExecutor taskWrapper, ThreadFactory delegate) Creates a thread factory which executes the thread task via the given task wrapping executor.
-
Method Details
-
directExecutor
Get the direct executor. This executor will immediately run any task it is given, and propagate back any run-time exceptions thrown.- Returns:
- the direct executor instance
-
directExecutorService
Get the direct executor service. This executor will immediately run any task it is given, and propagate back any run-time exceptions thrown. It cannot be shut down.- Returns:
- the direct executor service instance
-
rejectingExecutor
Get the rejecting executor. This executor will reject any task submitted to it.- Returns:
- the rejecting executor instance
-
rejectingExecutor
Get a rejecting executor. This executor will reject any task submitted to it with the given message.- Parameters:
message- the reject message- Returns:
- the rejecting executor instance
-
rejectingExecutorService
Get the rejecting executor service. This executor will reject any task submitted to it. It cannot be shut down.- Returns:
- the rejecting executor service instance
-
rejectingExecutorService
Get the rejecting executor service. This executor will reject any task submitted to it with the given message. It cannot be shut down.- Parameters:
message- the reject message- Returns:
- the rejecting executor service instance
-
discardingExecutor
Get the discarding executor. This executor will silently discard any task submitted to it.- Returns:
- the discarding executor instance
-
discardingExecutorService
Get the discarding executor service. This executor will silently discard any task submitted to it. It cannot be shut down.- Returns:
- the discarding executor service instance
-
privilegedExecutor
Create a direct executor which runs with the privileges given by the current access control context.- Parameters:
delegate- the executor to delegate to at the privileged level- Returns:
- the new direct executor
-
contextClassLoaderExecutor
public static DirectExecutor contextClassLoaderExecutor(DirectExecutor delegate, ClassLoader taskClassLoader) Create a direct executor which runs tasks with the given context class loader.- Parameters:
delegate- the executor to delegate totaskClassLoader- the context class loader to use- Returns:
- the new direct executor
-
threadNameExecutor
Create a direct executor which changes the thread name for the duration of a task.- Parameters:
delegate- the executor to delegate tonewName- the thread name to use- Returns:
- the new direct executor
-
threadFormattedNameExecutor
Create a direct executor which changes the thread name for the duration of a task using a formatted name. The thread must be aJBossThread.- Parameters:
delegate- the executor to delegate tonewName- the thread name to use- Returns:
- the new direct executor
-
threadNameNotateExecutor
Create a direct executor which adds a note to the thread name for the duration of a task.- Parameters:
delegate- the executor to delegate tonotation- the note to use- Returns:
- the new direct executor
-
exceptionLoggingExecutor
public static DirectExecutor exceptionLoggingExecutor(DirectExecutor delegate, org.jboss.logging.Logger log) Create a direct executor which consumes and logs errors that are thrown.- Parameters:
delegate- the executor to delegate tolog- the logger to which exceptions are written at theerrorlevel- Returns:
- the new direct executor
-
exceptionLoggingExecutor
public static DirectExecutor exceptionLoggingExecutor(DirectExecutor delegate, org.jboss.logging.Logger log, org.jboss.logging.Logger.Level level) Create a direct executor which consumes and logs errors that are thrown.- Parameters:
delegate- the executor to delegate tolog- the logger to which exceptions are writtenlevel- the level at which to log exceptions- Returns:
- the new direct executor
-
exceptionLoggingExecutor
Create a direct executor which consumes and logs errors that are thrown to the default thread error category"org.jboss.threads.errors".- Parameters:
delegate- the executor to delegate to- Returns:
- the new direct executor
-
resettingExecutor
Create a direct executor which delegates tasks to the given executor, and then clears all thread-local data after each task completes (regardless of outcome). You must have theRuntimePermission("modifyThread")permission to use this method.- Parameters:
delegate- the delegate direct executor- Returns:
- a resetting executor
- Throws:
SecurityException- if the caller does not have theRuntimePermission("modifyThread")permission
-
initializingExecutor
Create an executor which runs the given initialization task before running its given task.- Parameters:
delegate- the delegate direct executorinitializer- the initialization task- Returns:
- an initializing executor
-
cleanupExecutor
Create an executor which runs the given cleanup task after running its given task.- Parameters:
delegate- the delegate direct executorcleaner- the cleanup task- Returns:
- an initializing executor
-
blockingDirectExecutor
Deprecated.Get an executor which executes tasks in the current thread, which implementsBlockingExecutor.- Returns:
- the blocking direct executor
-
blockingDiscardingExecutor
Deprecated.Get an executor which discards all tasks, which implementsBlockingExecutor.- Returns:
- the executor
-
blockingRejectingExecutor
Deprecated.Get an executor which rejects all tasks, which implementsBlockingExecutor.- Returns:
- the executor
-
wrappingExecutor
An executor which delegates to another executor, wrapping each task in a task wrapper.- Parameters:
taskWrapper- the task wrapperdelegate- the delegate executor- Returns:
- a wrapping executor
-
directBlockingExecutor
Deprecated.An executor which delegates to the given direct executor, but implements the blocking executor interface. Since direct executors always execute tasks in the current thread, no blocking is possible; therefore the methods of BlockingExecutors always succeed or fail instantly.- Parameters:
delegate- the delegate direct executor- Returns:
- the blocking executor
-
wrappingExecutor
Create a wrapping executor for a delegate executor which creates anexecutorTask(DirectExecutor, Runnable)for each task.- Parameters:
delegate- the delegate executor- Returns:
- the wrapping executor
-
executor
An executor which delegates to a wrapping executor, wrapping each task in a task wrapper.- Parameters:
delegate- the delegate executortaskWrapper- the task wrapper- Returns:
- a wrapping executor
-
threadFactoryExecutor
Deprecated.UseEnhancedQueueExecutorinstead.Create an executor that executes each task in a new thread.- Parameters:
factory- the thread factory to use- Returns:
- the executor
-
threadFactoryExecutor
@Deprecated public static BlockingExecutor threadFactoryExecutor(ThreadFactory factory, int maxThreads) Deprecated.UseEnhancedQueueExecutorinstead.Create an executor that executes each task in a new thread. By default up to the given number of threads may run concurrently, after which new tasks will be rejected.- Parameters:
factory- the thread factory to usemaxThreads- the maximum number of allowed threads- Returns:
- the executor
-
threadFactoryExecutor
@Deprecated public static BlockingExecutor threadFactoryExecutor(ThreadFactory factory, int maxThreads, boolean blocking) Deprecated.UseEnhancedQueueExecutorinstead.Create an executor that executes each task in a new thread. By default up to the given number of threads may run concurrently, after which the caller will block or new tasks will be rejected, according to the setting of theblockingparameter.- Parameters:
factory- the thread factory to usemaxThreads- the maximum number of allowed threadsblocking-trueif the submitter should block when the maximum number of threads has been reached- Returns:
- the executor
-
threadFactoryExecutor
@Deprecated public static BlockingExecutor threadFactoryExecutor(ThreadFactory factory, int maxThreads, boolean blocking, DirectExecutor taskExecutor) Deprecated.UseEnhancedQueueExecutorinstead.Create an executor that executes each task in a new thread. By default up to the given number of threads may run concurrently, after which the caller will block or new tasks will be rejected, according to the setting of theblockingparameter.- Parameters:
factory- the thread factory to usemaxThreads- the maximum number of allowed threadsblocking-trueif the submitter should block when the maximum number of threads has been reachedtaskExecutor- the executor which should run each task- Returns:
- the executor
-
abortPolicy
Get the abort policy for aThreadPoolExecutor.- Returns:
- the abort policy
- See Also:
-
callerRunsPolicy
Get the caller-runs policy for aThreadPoolExecutor.- Returns:
- the caller-runs policy
- See Also:
-
discardOldestPolicy
Get the discard-oldest policy for aThreadPoolExecutor.- Returns:
- the discard-oldest policy
- See Also:
-
discardPolicy
Get the discard policy for aThreadPoolExecutor.- Returns:
- the discard policy
- See Also:
-
handoffPolicy
Get a handoff policy for aThreadPoolExecutor. The returned instance will delegate to another executor in the event that the task is rejected.- Parameters:
target- the target executor- Returns:
- the new handoff policy implementation
-
protectedBlockingExecutor
Deprecated. -
protectedExecutorService
Wrap an executor with anExecutorServiceinstance which supports all the features ofExecutorServiceexcept for shutting down the executor.- Parameters:
target- the target executor- Returns:
- the executor service
-
protectedDirectExecutorService
Wrap a direct executor with anDirectExecutorServiceinstance which supports all the features ofExecutorServiceexcept for shutting down the executor.- Parameters:
target- the target executor- Returns:
- the executor service
-
protectedScheduledExecutorService
public static ScheduledExecutorService protectedScheduledExecutorService(ScheduledExecutorService target) Wrap a scheduled executor with aScheduledExecutorServiceinstance which supports all the features ofScheduledExecutorServiceexcept for shutting down the executor.- Parameters:
target- the target executor- Returns:
- the executor service
-
protectedBlockingExecutorService
@Deprecated public static BlockingExecutorService protectedBlockingExecutorService(BlockingExecutor target) Deprecated.Wrap a blocking executor with anBlockingExecutorServiceinstance which supports all the features ofBlockingExecutorServiceexcept for shutting down the executor.- Parameters:
target- the target executor- Returns:
- the executor service
-
resettingThreadFactory
Create a thread factory which resets all thread-local storage and delegates to the given thread factory. You must have theRuntimePermission("modifyThread")permission to use this method.- Parameters:
delegate- the delegate thread factory- Returns:
- the resetting thread factory
- Throws:
SecurityException- if the caller does not have theRuntimePermission("modifyThread")permission
-
wrappingThreadFactory
public static ThreadFactory wrappingThreadFactory(DirectExecutor taskWrapper, ThreadFactory delegate) Creates a thread factory which executes the thread task via the given task wrapping executor.- Parameters:
taskWrapper- the task wrapping executordelegate- the delegate thread factory- Returns:
- the wrapping thread factory
-
nullRunnable
Get the null runnable which does nothing.- Returns:
- the null runnable
-
threadLocalResetter
Get aRunnablewhich, when executed, clears the thread-local storage of the calling thread. You must have theRuntimePermission("modifyThread")permission to use this method.- Returns:
- the runnable
- Throws:
SecurityException- if the caller does not have theRuntimePermission("modifyThread")permission
-
contextClassLoaderResetter
Get aRunnablewhich, when executed, clears the thread context class loader (if the caller has sufficient privileges).- Returns:
- the runnable
-
executorTask
Get a task that runs the given task through the given direct executor.- Parameters:
executor- the executor to run the task throughtask- the task to run- Returns:
- an encapsulating task
-
compositeTask
-
classLoaderPreservingTask
Create a task that delegates to the given task, preserving the context classloader which was in effect when this method was invoked.- Parameters:
delegate- the delegate runnable- Returns:
- the wrapping runnable
- Throws:
SecurityException- if a security manager exists and the caller does not have the"copyClassLoader"RuntimePermission.
-
compositeTask
Create a task that is a composite of several other tasks.- Parameters:
runnables- the tasks- Returns:
- the composite task
-
loggingExceptionHandler
Get an uncaught exception handler which logs to the given logger.- Parameters:
log- the logger- Returns:
- the handler
-
loggingExceptionHandler
Get an uncaught exception handler which logs to the given logger.- Parameters:
categoryName- the name of the logger category to log to- Returns:
- the handler
-
loggingExceptionHandler
Get an uncaught exception handler which logs to the default error logger.- Returns:
- the handler
-
run
public static <R extends Runnable, A> void run(R task, DirectExecutor directExecutor, TaskNotifier<? super R, ? super A> notifier, A attachment) Run a task through the given direct executor, invoking the given notifier with the given attachment.- Type Parameters:
R- the task typeA- the attachment type- Parameters:
task- the taskdirectExecutor- the executornotifier- the notifierattachment- the attachment
-
run
public static <R extends Runnable, A> void run(R task, TaskNotifier<? super R, ? super A> notifier, A attachment) Run a task, invoking the given notifier with the given attachment.- Type Parameters:
R- the task typeA- the attachment type- Parameters:
task- the tasknotifier- the notifierattachment- the attachment
-
notifyingRunnable
public static <R extends Runnable, A> Runnable notifyingRunnable(R task, TaskNotifier<? super R, ? super A> notifier, A attachment) Get a notifying runnable wrapper for a task. The notifier will be invoked when the task is run.- Type Parameters:
R- the task typeA- the attachment type- Parameters:
task- the tasknotifier- the notifierattachment- the attachment- Returns:
- the wrapping runnable
-
notifyingDirectExecutor
public static <A> DirectExecutor notifyingDirectExecutor(DirectExecutor delegate, TaskNotifier<Runnable, ? super A> notifier, A attachment) Get a notifying direct executor. The notifier will be invoked when each task is run.- Type Parameters:
A- the attachment type- Parameters:
delegate- the executor which will actually run the tasknotifier- the notifierattachment- the attachment- Returns:
- the direct executor
-
executeUninterruptibly
public static void executeUninterruptibly(Executor executor, Runnable task) throws RejectedExecutionException Execute a task uninterruptibly.- Parameters:
executor- the executor to delegate totask- the task to execute- Throws:
RejectedExecutionException- if the task was rejected by the executor
-
uninterruptibleExecutor
-
dependencyTaskBuilder
Create a builder for a dependent task.- Parameters:
executor- the executor to usetask- the task to run when all dependencies are met- Returns:
- the builder
-
EnhancedQueueExecutorinstead.