Core pool size vs maximum pool size in ThreadPoolExecutor

后端 未结 10 1014
滥情空心
滥情空心 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:59

    java.util.concurrent.ThreadPoolExecutor

      public void execute(Runnable command) {
            if (command == null)
                throw new NullPointerException();
            /*
             * Proceed in 3 steps:
             *
             * 1. If fewer than corePoolSize threads are running, try to
             * start a new thread with the given command as its first
             * task.  The call to addWorker atomically checks runState and
             * workerCount, and so prevents false alarms that would add
             * threads when it shouldn't, by returning false.
             *
             * 2. If a task can be successfully queued, then we still need
             * to double-check whether we should have added a thread
             * (because existing ones died since last checking) or that
             * the pool shut down since entry into this method. So we
             * recheck state and if necessary roll back the enqueuing if
             * stopped, or start a new thread if there are none.
             *
             * 3. If we cannot queue task, then we try to add a new
             * thread.  If it fails, we know we are shut down or saturated
             * and so reject the task.
             */
            int c = ctl.get();
            if (workerCountOf(c) < corePoolSize) {
                if (addWorker(command, true))
                    return;
                c = ctl.get();
            }
            if (isRunning(c) && workQueue.offer(command)) {
                int recheck = ctl.get();
                if (! isRunning(recheck) && remove(command))
                    reject(command);
                else if (workerCountOf(recheck) == 0)
                    addWorker(null, false);
            }
            else if (!addWorker(command, false))
                reject(command);
        }
    
    0 讨论(0)
  • 2020-11-29 17:02

    Source

    Rules of a ThreadPoolExecutor pool size

    The rules for the size of a ThreadPoolExecutor's pool are generally miss-understood, because it doesn't work the way that you think it ought to or in the way that you want it to.

    Take this example. Starting thread pool size is 1, core pool size is 5, max pool size is 10 and the queue is 100.

    Sun's way: 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. 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.

    User anticipated way: as requests come in threads will be created up to 10, then tasks will be added to the queue until it reaches 100 at which point they are rejected. The number of threads will rename at max until the queue is empty. When the queue is empty the threads will die off until there are corePoolSize left.

    The difference is that the users want to start increasing the pool size earlier and want the queue to be smaller, where as the Sun method want to keep the pool size small and only increase it once the load becomes to much.

    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 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. The long and the short of it is that new threads are only created when the queue fills up, so if you're using an unbounded queue then the number of threads will not exceed corePoolSize.

    For a fuller explanation, get it from the horses mouth: ThreadPoolExecutor API documentation.

    There is a really good forum post which talks you through the way that the ThreadPoolExecutor works with code examples: http://forums.sun.com/thread.jspa?threadID=5401400&tstart=0

    More info: http://forums.sun.com/thread.jspa?threadID=5224557&tstart=450

    0 讨论(0)
  • 2020-11-29 17:02

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

    The link above has the answer to your question. However, just to make it clear. The application will keep creating threads till it reaches the corePoolSize. I think the idea here is that these many threads should be sufficient to handle the inflow of tasks. If a new task comes after the corePoolSize threads are created the tasks will be queued. Once the queue is full the executor will start creating new threads. It is kind of balancing. What it essentially means is that the inflow of tasks is more than the processing capacity. So, Executor will start creating new threads again till it reaches Max number of threads. Again, a new threads will be created if and only if the queue is full.

    0 讨论(0)
  • 2020-11-29 17:07

    From the book Java concurency essentials :

    CorePoolSize: The ThreadPoolExecutor has an attribute corePoolSize that determines how many threads it will start until new threads are only started when the queue is full

    MaximumPoolSize: This attribute determines how many threads are started at the maximum. You can set this to Integer. MAX_VALUE in order to have no upper boundary

    0 讨论(0)
提交回复
热议问题