Class JBossExecutors

java.lang.Object
org.jboss.threads.JBossExecutors

public final class JBossExecutors extends Object
JBoss thread- and executor-related utility and factory methods.
  • Method Details

    • directExecutor

      public static DirectExecutor 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

      public static DirectExecutorService 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

      public static DirectExecutor rejectingExecutor()
      Get the rejecting executor. This executor will reject any task submitted to it.
      Returns:
      the rejecting executor instance
    • rejectingExecutor

      public static DirectExecutor rejectingExecutor(String message)
      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

      public static DirectExecutorService 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

      public static DirectExecutorService rejectingExecutorService(String message)
      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

      public static DirectExecutor discardingExecutor()
      Get the discarding executor. This executor will silently discard any task submitted to it.
      Returns:
      the discarding executor instance
    • discardingExecutorService

      public static DirectExecutorService 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

      public static DirectExecutor privilegedExecutor(DirectExecutor delegate)
      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 to
      taskClassLoader - the context class loader to use
      Returns:
      the new direct executor
    • threadNameExecutor

      public static DirectExecutor threadNameExecutor(DirectExecutor delegate, String newName)
      Create a direct executor which changes the thread name for the duration of a task.
      Parameters:
      delegate - the executor to delegate to
      newName - the thread name to use
      Returns:
      the new direct executor
    • threadFormattedNameExecutor

      public static DirectExecutor threadFormattedNameExecutor(DirectExecutor delegate, String newName)
      Create a direct executor which changes the thread name for the duration of a task using a formatted name. The thread must be a JBossThread.
      Parameters:
      delegate - the executor to delegate to
      newName - the thread name to use
      Returns:
      the new direct executor
    • threadNameNotateExecutor

      public static DirectExecutor threadNameNotateExecutor(DirectExecutor delegate, String notation)
      Create a direct executor which adds a note to the thread name for the duration of a task.
      Parameters:
      delegate - the executor to delegate to
      notation - 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 to
      log - the logger to which exceptions are written at the error level
      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 to
      log - the logger to which exceptions are written
      level - the level at which to log exceptions
      Returns:
      the new direct executor
    • exceptionLoggingExecutor

      public static DirectExecutor exceptionLoggingExecutor(DirectExecutor delegate)
      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

      public static DirectExecutor resettingExecutor(DirectExecutor delegate) throws SecurityException
      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 the RuntimePermission("modifyThread") permission to use this method.
      Parameters:
      delegate - the delegate direct executor
      Returns:
      a resetting executor
      Throws:
      SecurityException - if the caller does not have the RuntimePermission("modifyThread") permission
    • initializingExecutor

      public static DirectExecutor initializingExecutor(DirectExecutor delegate, Runnable initializer)
      Create an executor which runs the given initialization task before running its given task.
      Parameters:
      delegate - the delegate direct executor
      initializer - the initialization task
      Returns:
      an initializing executor
    • cleanupExecutor

      public static DirectExecutor cleanupExecutor(DirectExecutor delegate, Runnable cleaner)
      Create an executor which runs the given cleanup task after running its given task.
      Parameters:
      delegate - the delegate direct executor
      cleaner - the cleanup task
      Returns:
      an initializing executor
    • blockingDirectExecutor

      @Deprecated public static BlockingExecutor blockingDirectExecutor()
      Deprecated.
      Get an executor which executes tasks in the current thread, which implements BlockingExecutor.
      Returns:
      the blocking direct executor
    • blockingDiscardingExecutor

      @Deprecated public static BlockingExecutor blockingDiscardingExecutor()
      Deprecated.
      Get an executor which discards all tasks, which implements BlockingExecutor.
      Returns:
      the executor
    • blockingRejectingExecutor

      @Deprecated public static BlockingExecutor blockingRejectingExecutor()
      Deprecated.
      Get an executor which rejects all tasks, which implements BlockingExecutor.
      Returns:
      the executor
    • wrappingExecutor

      public static Executor wrappingExecutor(DirectExecutor taskWrapper, Executor delegate)
      An executor which delegates to another executor, wrapping each task in a task wrapper.
      Parameters:
      taskWrapper - the task wrapper
      delegate - the delegate executor
      Returns:
      a wrapping executor
    • directBlockingExecutor

      @Deprecated public static BlockingExecutor directBlockingExecutor(DirectExecutor delegate)
      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

      public static WrappingExecutor wrappingExecutor(Executor delegate)
      Create a wrapping executor for a delegate executor which creates an executorTask(DirectExecutor, Runnable) for each task.
      Parameters:
      delegate - the delegate executor
      Returns:
      the wrapping executor
    • executor

      public static Executor executor(WrappingExecutor delegate, DirectExecutor taskWrapper)
      An executor which delegates to a wrapping executor, wrapping each task in a task wrapper.
      Parameters:
      delegate - the delegate executor
      taskWrapper - the task wrapper
      Returns:
      a wrapping executor
    • threadFactoryExecutor

      @Deprecated public static BlockingExecutor threadFactoryExecutor(ThreadFactory factory)
      Deprecated.
      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.
      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 use
      maxThreads - the maximum number of allowed threads
      Returns:
      the executor
    • threadFactoryExecutor

      @Deprecated public static BlockingExecutor threadFactoryExecutor(ThreadFactory factory, int maxThreads, boolean blocking)
      Deprecated.
      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 the blocking parameter.
      Parameters:
      factory - the thread factory to use
      maxThreads - the maximum number of allowed threads
      blocking - true if 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.
      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 the blocking parameter.
      Parameters:
      factory - the thread factory to use
      maxThreads - the maximum number of allowed threads
      blocking - true if the submitter should block when the maximum number of threads has been reached
      taskExecutor - the executor which should run each task
      Returns:
      the executor
    • abortPolicy

      public static RejectedExecutionHandler abortPolicy()
      Get the abort policy for a ThreadPoolExecutor.
      Returns:
      the abort policy
      See Also:
    • callerRunsPolicy

      public static RejectedExecutionHandler callerRunsPolicy()
      Get the caller-runs policy for a ThreadPoolExecutor.
      Returns:
      the caller-runs policy
      See Also:
    • discardOldestPolicy

      public static RejectedExecutionHandler discardOldestPolicy()
      Get the discard-oldest policy for a ThreadPoolExecutor.
      Returns:
      the discard-oldest policy
      See Also:
    • discardPolicy

      public static RejectedExecutionHandler discardPolicy()
      Get the discard policy for a ThreadPoolExecutor.
      Returns:
      the discard policy
      See Also:
    • handoffPolicy

      public static RejectedExecutionHandler handoffPolicy(Executor target)
      Get a handoff policy for a ThreadPoolExecutor. 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 public static BlockingExecutor protectedBlockingExecutor(BlockingExecutor target)
      Deprecated.
    • protectedExecutorService

      public static ExecutorService protectedExecutorService(Executor target)
      Wrap an executor with an ExecutorService instance which supports all the features of ExecutorService except for shutting down the executor.
      Parameters:
      target - the target executor
      Returns:
      the executor service
    • protectedDirectExecutorService

      public static DirectExecutorService protectedDirectExecutorService(DirectExecutor target)
      Wrap a direct executor with an DirectExecutorService instance which supports all the features of ExecutorService except 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 a ScheduledExecutorService instance which supports all the features of ScheduledExecutorService except 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 an BlockingExecutorService instance which supports all the features of BlockingExecutorService except for shutting down the executor.
      Parameters:
      target - the target executor
      Returns:
      the executor service
    • resettingThreadFactory

      public static ThreadFactory resettingThreadFactory(ThreadFactory delegate) throws SecurityException
      Create a thread factory which resets all thread-local storage and delegates to the given thread factory. You must have the RuntimePermission("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 the RuntimePermission("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 executor
      delegate - the delegate thread factory
      Returns:
      the wrapping thread factory
    • nullRunnable

      public static Runnable nullRunnable()
      Get the null runnable which does nothing.
      Returns:
      the null runnable
    • threadLocalResetter

      public static Runnable threadLocalResetter() throws SecurityException
      Get a Runnable which, when executed, clears the thread-local storage of the calling thread. You must have the RuntimePermission("modifyThread") permission to use this method.
      Returns:
      the runnable
      Throws:
      SecurityException - if the caller does not have the RuntimePermission("modifyThread") permission
    • contextClassLoaderResetter

      public static Runnable contextClassLoaderResetter()
      Get a Runnable which, when executed, clears the thread context class loader (if the caller has sufficient privileges).
      Returns:
      the runnable
    • executorTask

      public static Runnable executorTask(DirectExecutor executor, Runnable task)
      Get a task that runs the given task through the given direct executor.
      Parameters:
      executor - the executor to run the task through
      task - the task to run
      Returns:
      an encapsulating task
    • compositeTask

      public static Runnable compositeTask(Runnable... runnables)
      Create a task that is a composite of several other tasks.
      Parameters:
      runnables - the tasks
      Returns:
      the composite task
    • classLoaderPreservingTask

      public static Runnable classLoaderPreservingTask(Runnable delegate) throws SecurityException
      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

      public static Runnable compositeTask(Collection<Runnable> runnables)
      Create a task that is a composite of several other tasks.
      Parameters:
      runnables - the tasks
      Returns:
      the composite task
    • loggingExceptionHandler

      public static Thread.UncaughtExceptionHandler loggingExceptionHandler(org.jboss.logging.Logger log)
      Get an uncaught exception handler which logs to the given logger.
      Parameters:
      log - the logger
      Returns:
      the handler
    • loggingExceptionHandler

      public static Thread.UncaughtExceptionHandler loggingExceptionHandler(String categoryName)
      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

      public static Thread.UncaughtExceptionHandler 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 type
      A - the attachment type
      Parameters:
      task - the task
      directExecutor - the executor
      notifier - the notifier
      attachment - 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 type
      A - the attachment type
      Parameters:
      task - the task
      notifier - the notifier
      attachment - 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 type
      A - the attachment type
      Parameters:
      task - the task
      notifier - the notifier
      attachment - 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 task
      notifier - the notifier
      attachment - 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 to
      task - the task to execute
      Throws:
      RejectedExecutionException - if the task was rejected by the executor
    • uninterruptibleExecutor

      public static Executor uninterruptibleExecutor(Executor delegate)
      Get an executor which executes tasks uninterruptibly in the event of blocking.
      Parameters:
      delegate - the delegate executor
      Returns:
      the uninterruptible executor
    • dependencyTaskBuilder

      public static DependencyTaskBuilder dependencyTaskBuilder(Executor executor, Runnable task)
      Create a builder for a dependent task.
      Parameters:
      executor - the executor to use
      task - the task to run when all dependencies are met
      Returns:
      the builder