Thread Pools in Java

Rules for thread pool creation

Here are Sun's rules for thread creation in simple terms:

  1. If the number of threads is less than the corePoolSize, create a new Thread to run a new task.
  2. If the number of threads is equal (or greater than) the corePoolSize, put the task into the queue.
  3. If thread count exceeds queueCapacity, and the number of threads is less than the maxPoolSize, create a new thread to run tasks in.
  4. If the queue is full, and the number of threads is greater than or equal to maxPoolSize, reject the task.

Example

Question:

How threads will be created with below configuration:

  • Starting thread pool size is 1
  • Core pool size is 5
  • Max pool size is 10
  • Queue is 100.
  • Answer:

  • As requests come in threads will be created up to 5
  • Then tasks will be added to the queue until it reaches 100.
  • When the queue is full new threads will be created up to maxPoolSize(10).
  • Once all the threads are in use and the queue is full tasks will be rejected. As the queue reduces so does the number of active threads.
  • Snippet

    {
        executor.setCorePoolSize(100);
        executor.setQueueCapacity(10000);
        executor.setMaxPoolSize(250);
        executor.setThreadNamePrefix("asyncThreadPool-");
        executor.initialize();
        return executor;
    }