Class ThreadPoolExecutor

All Implemented Interfaces:
ExecutorService, Executor, ExecutorService
Direct Known Subclasses:
TimeScheduledThreadPoolExecutor

public class ThreadPoolExecutor extends ThreadPoolExecutor implements ExecutorService
An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.
  • Constructor Details

    • ThreadPoolExecutor

      public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
      Creates a new ThreadPoolExecutor with the given initial parameters and default thread factory and rejected execution handler. It may be more convenient to use one of the Executors factory methods instead of this general purpose constructor.
      Parameters:
      corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
      maximumPoolSize - the maximum number of threads to allow in the pool
      keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
      unit - the time unit for the keepAliveTime argument
      workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
      Throws:
      IllegalArgumentException - if one of the following holds:
      corePoolSize < 0
      keepAliveTime < 0
      maximumPoolSize <= 0
      maximumPoolSize < corePoolSize
      NullPointerException - if workQueue is null
    • ThreadPoolExecutor

      public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
      Creates a new ThreadPoolExecutor with the given initial parameters and default rejected execution handler.
      Parameters:
      corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
      maximumPoolSize - the maximum number of threads to allow in the pool
      keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
      unit - the time unit for the keepAliveTime argument
      workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
      threadFactory - the factory to use when the executor creates a new thread
      Throws:
      IllegalArgumentException - if one of the following holds:
      corePoolSize < 0
      keepAliveTime < 0
      maximumPoolSize <= 0
      maximumPoolSize < corePoolSize
      NullPointerException - if workQueue or threadFactory is null
    • ThreadPoolExecutor

      public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
      Creates a new ThreadPoolExecutor with the given initial parameters and default thread factory.
      Parameters:
      corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
      maximumPoolSize - the maximum number of threads to allow in the pool
      keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
      unit - the time unit for the keepAliveTime argument
      workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
      handler - the handler to use when execution is blocked because the thread bounds and queue capacities are reached
      Throws:
      IllegalArgumentException - if one of the following holds:
      corePoolSize < 0
      keepAliveTime < 0
      maximumPoolSize <= 0
      maximumPoolSize < corePoolSize
      NullPointerException - if workQueue or handler is null
    • ThreadPoolExecutor

      public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
      Creates a new ThreadPoolExecutor with the given initial parameters.
      Parameters:
      corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
      maximumPoolSize - the maximum number of threads to allow in the pool
      keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
      unit - the time unit for the keepAliveTime argument
      workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
      threadFactory - the factory to use when the executor creates a new thread
      handler - the handler to use when execution is blocked because the thread bounds and queue capacities are reached
      Throws:
      IllegalArgumentException - if one of the following holds:
      corePoolSize < 0
      keepAliveTime < 0
      maximumPoolSize <= 0
      maximumPoolSize < corePoolSize
      NullPointerException - if workQueue or threadFactory or handler is null
  • Method Details

    • newTaskFor

      protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value)
      Returns a RunnableFuture for the given runnable and default value.
      Overrides:
      newTaskFor in class AbstractExecutorService
      Parameters:
      runnable - the runnable task being wrapped
      value - the default value for the returned future
      Returns:
      a RunnableFuture which, when run, will run the underlying runnable and which, as a Future, will yield the given value as its result and provide for cancellation of the underlying task
      Since:
      1.6
    • newTaskFor

      protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable)
      Returns a RunnableFuture for the given callable task.
      Overrides:
      newTaskFor in class AbstractExecutorService
      Parameters:
      callable - the callable task being wrapped
      Returns:
      a RunnableFuture which, when run, will call the underlying callable and which, as a Future, will yield the callable's result as its result and provide for cancellation of the underlying task
      Since:
      1.6
    • submit

      @Nonnull public AsyncFuture<?> submit(@Nonnull Runnable task)
      Description copied from interface: ExecutorService
      Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.
      Specified by:
      submit in interface ExecutorService
      Specified by:
      submit in interface ExecutorService
      Overrides:
      submit in class AbstractExecutorService
      Parameters:
      task - the task to submit
      Returns:
      a Future representing pending completion of the task
      Throws:
      RejectedExecutionException
      NullPointerException - if the task is null
    • submit

      @Nonnull public <T> AsyncFuture<T> submit(@Nonnull Runnable task, T result)
      Description copied from interface: ExecutorService
      Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return the given result upon successful completion.
      Specified by:
      submit in interface ExecutorService
      Specified by:
      submit in interface ExecutorService
      Overrides:
      submit in class AbstractExecutorService
      Type Parameters:
      T - the type of the result
      Parameters:
      task - the task to submit
      result - the result to return
      Returns:
      a Future representing pending completion of the task
      Throws:
      RejectedExecutionException
      NullPointerException - if the task is null
    • submit

      @Nonnull public <T> AsyncFuture<T> submit(@Nonnull Callable<T> task)
      Description copied from interface: ExecutorService
      Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future's get method will return the task's result upon successful completion.

      If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();

      Note: The Executors class includes a set of methods that can convert some other common closure-like objects, for example, PrivilegedAction to Callable form so they can be submitted.

      Specified by:
      submit in interface ExecutorService
      Specified by:
      submit in interface ExecutorService
      Overrides:
      submit in class AbstractExecutorService
      Type Parameters:
      T - the type of the task's result
      Parameters:
      task - the task to submit
      Returns:
      a Future representing pending completion of the task
      Throws:
      RejectedExecutionException
      NullPointerException - if the task is null