Core pool size vs maximum pool size in ThreadPoolExecutor

后端 未结 10 1072
滥情空心
滥情空心 2020-11-29 16:08

What exactly is the difference between core pool size and maximum pool size when we talk in terms of ThreadPoolExecutor?
C

10条回答
  •  被撕碎了的回忆
    2020-11-29 16:46

    IF running threads > corePoolSize & < maxPoolSize, then create a new Thread if Total task queue is full and new one is arriving.

    Form doc: (If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.)

    Now, Take a simple example,

    ThreadPoolExecutor executorPool = new ThreadPoolExecutor(5, 10, 3, TimeUnit.SECONDS, new ArrayBlockingQueue(50));
    

    Here, 5 is the corePoolSize - means Jvm will create new thread for new task for first 5 tasks. and other tasks will be added to the queue until queue is getting full (50 tasks).

    10 is the maxPoolSize - JVM can create max 10 threads. Means if there are already 5 task/thread is running and queue is full with 50 pending tasks and if one more new request/task is arriving in queue then JVM will create new thread up to 10 (total threads=previous 5 + new 5);

    new ArrayBlockingQueue(50) = is a total queue size - it can queue 50 tasks in it.

    once all 10 threads are running and if new task is arriving then that new task will be rejected.

    Rules for creating Threads internally by SUN:

    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 the queue is full, 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.

    Hope, This is HelpFul.. and please correct me if i'm wrong...

提交回复
热议问题