ThreadPoolExecutor - kernel and maximum pool sizes

When a new task is dispatched in the execute(java.lang.Runnable) method execute(java.lang.Runnable) and fewer corePoolSize threads are corePoolSize , a new thread is created to process the request, even if other worker threads are idle.

1) Why is there a need to create a new thread to process the request, if there are idle threads?

If there is more than corePoolSize but less than maximumPoolSize threads, a new thread will only be created if the queue is full.

2) I do not understand the difference between corePoolSize and maximumPoolSize here. Secondly, how can a queue be filled when threads are less than maximumPoolSize ? A queue can only be populated if the threads are equal to or greater than maximumPoolSize . Is not it?

+12
java threadpoolexecutor
Jul 15 '13 at 17:20
source share
4 answers

Here are the Sun rules for creating streams in simple terms:

  • If the number of threads is less than corePoolSize, create a new thread to start a new task.
  • If the number of threads is equal to (or greater than) corePoolSize, put the task in the queue.
  • If the queue is full and the number of threads is less than maxPoolSize, create a new thread to start the tasks.
  • If the queue is full and the number of threads is greater than or equal to maxPoolSize, reject the task.

Full article

+13
Jan 27 '16 at 4:37
source share
— -

You can find the definition of the terms corepoolsize and maxpoolsize in javadoc. http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html

The link above has an answer to your question. However, just to make it clear. The application will create threads until it reaches corePoolSize. This means that this number of threads must be sufficient to handle the influx of tasks. After that, the tasks will be queued. As soon as the queue is full, the executor will begin to create new threads. This is a kind of balancing. In essence, this means that the influx of tasks is greater than bandwidth. Thus, Executor will begin to create new threads again until it reaches the maximum number of threads. Again, new threads will be created if and only when the queue is full.

+8
Sep 02 '13 at 10:37
source share

Primary and maximum pool sizes

ThreadPoolExecutor automatically adjusts the pool size to the limits set by corePoolSize and maximumPoolSize.

When a new task is dispatched in the execute(java.lang.Runnable) method execute(java.lang.Runnable) and fewer corePoolSize threads are executed, a new thread is created to process the request, even if other worker threads are idle. If there is more than corePoolSize, but less than maximumPoolSize threads are running, a new thread will only be created if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool.

By setting maxPoolSize to a substantially unlimited value, such as Integer.MAX_VALUE , you allow the pool to accommodate an arbitrary number of simultaneous tasks. As a rule, the kernel and maximum pool sizes are set only during construction, but they can also be dynamically changed using setCorePoolSize(int) and setMaximumPoolSize(int) . link

+5
Jan 29 '14 at 22:16
source share

Take this example. The initial thread pool size is 1, the pool pool size is 5, the maximum pool size is 10, and the queue is 100. As requests arrive in the threads, up to 5 will be created, then tasks will be added to the queue until 100 is reached. When the queue will be filled with new threads until maxPoolSize. After all threads are used and the queue is full, tasks will be rejected. As the queue decreases, the number of active threads also decreases.

+3
Jul 16 '13 at 5:32
source share



All Articles