Java Thread Pool (ExecutorService)

Java concurrent package ExecutorService interface provides thread pool support. Different types of thread pools (ExecutorService) can be created using Executors class’s following static functions

  • newCachedThreadPool()
    • Create a new thread for a new submitted task (callable or future). Idle thread will be terminated after 60 seconds.
  • newFixedThreadPool(int nThreads)
    • Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue
  • newSingleThreadExecutor()
    • Creates an Executor that uses a single worker thread operating off an unbounded queue
  • newScheduledThreadPool(int corePoolSize)
    • Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically
  • newWorkStealPool(int parellelism)
    • Creates a thread pool that maintains enough threads to support the given parallelism level, and may use multiple queues to reduce contention.
    • Note that, this means each thread may have its own task queue, and other thread can “steal” task from neighbor thread if needed.
    • This function actual create ForkJoinPool, i.e.,
public static ExecutorService newWorkStealingPool() {
    return new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
                            ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                            null, true);
}
Advertisements
Java Thread Pool (ExecutorService)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s