KeepAliveTime meaning

After reading a lot of articles, I feel that the meaning of keepAliveTime can not be very clear. I hope to make keepAliveTime clear through my own understanding

When the idle time of a thread reaches keepAliveTime, the thread exits

It’s hard to understand keepAliveTime if you can’t keep track of all the thread pool parameters

Let’s take an example:

The number of core threads is 10, the maximum number of threads is 30, and the keepAliveTime is 3 seconds

As the number of tasks increases, the thread pool will continue to create threads until it reaches the core number of threads 10, and no more threads will be created.

The extra tasks are then added to the blocking queue to run,

When the blocking queue length + the number of core threads is exceeded,

When you have to expand the number of threads to accommodate the current task, you need to create new threads (the maximum number of threads counts), up to a maximum of 30

The 20 threads that may be created that exceed the core thread count by 10 and are less than the maximum thread count of 30 are “borrowed” and are exited if they are idle for more than keepAliveTime

Let’s look at the first two questions:

1. Why are threads idle

2. Why should the thread exit

A:

Threads are idle when there are no tasks. In the thread pool, tasks are tasks (Runnale) and threads are threads (Worker).

2, usually beyond core thread thread is “borrow”, is a thread that is beyond the core is able to foresee the abnormal situation, and this kind of situation doesn’t happen very often (if often happens, that I think you should adjust your core number of threads), so this does not happen very often and create threads in order to avoid the waste of resources should be to exit

We need to have a look at the Java. Util. Concurrent. ThreadPoolExecutor# getTask source to verify the above the meaning of a passage:

int wc = workerCountOf(c); // Are workers subject to culling? If allowCoreThreadTimeOut is true or the number of current tasks exceeds the number of core threads, Timed to true Boolean timed = allowCoreThreadTimeOut | | wc > corePoolSize; ... try { Runnable r = timed ? Poll (keepAliveTime, timeunit.nanoseconds) :// If timed is true, the worker may be shut down. Workqueue.take () : workQueue.take() : workQueue.take() : workqueue.take () : workqueue.take (); // Otherwise the task queue is empty and blocks until there is another task in the task queue. = null) return r;Copy the code

The 10 core threads will exit or not, depending on the following parameters:

AllowCoreThreadTimeout: Whether to allow the core thread to exit idle. Default is false

When keepAliveTime is set to 0, whether idle threads exit directly or not

Poll (keepAliveTime, timeunit.nanoseconds) shows that it does not wait to exit directly, rather than never exit

It is important to note that setting this value to 0 is not a good practice (unless the number of tasks in the scenario rarely exceeds the number of core threads). If the number of tasks frequently exceeds the number of core threads, this value should be evaluated as a reasonable value to avoid thread opening and closing actions

(Above, workers would be 45 instead of 10 if they never quit)