Process thread pool to perform tasks

After the thread pool is how to work it, to borrow a book of art concurrent programming words to describe when a task submitted to the thread pool thread pool will be how to do?

First of all, the thread pool will determine the core thread pool thread (total number of threads is 30, then there may be coreSize 10) whether all the tasks. If you do not say that only 9 of the current thread creates a new thread in the work, from the core of the thread pool to perform the task. If you currently have 10 threads at work, and then enter the next step;

secondly, the thread pool will determine whether the work queue is full, if the work queue is not full, the task will be stored in the newly submitted job queue, if the work queue is full, then enter the next process;

and finally, the thread pool whether all the threads are working, and if not, that is, 30 threads only 25 work, create a new worker thread to perform the task, if you already have 30 threads to execute, there is no idle thread,

then to the saturation strategy to deal with this task (the default saturation strategy to throw an exception).
After submitting the task will first try to the core thread pool thread to do, but a limited number of threads must be the core of the thread pool, it is necessary to do a buffer queue by the task, the first task to put the queue first, and then waiting thread to execute, finally, because too many tasks, the queue was full, and this time the rest of the thread pool thread will start a thread pool to help core mission. If still no way to properly deal with the newly arrived task, the task of the new thread pool can only be submitted to the saturation strategy to deal with.

Create a new thread pool ThreadPoolExecutor (corePoolSize, maximumPoolSize, keepAliveTime, milliseconds,

runnableTaskQueue, handler);

The last parameter handler is a saturation strategy, java thread pool framework can provide four kinds of strategies:

1) AbortPolicy: direct throw an exception

2) CallerRunsPolicy: where only the caller thread to run the task

3) DiscardOldestPolicy: recently dropped from the queue of a task, and execute the current task.

4) DiscardPolicy: no treatment, discarded.

Finally, we include a thread pool max = 5, core = 3, the task queue taskQueue = 5; with saturated strategy 1)

then we look at this execution logic to submit the task to the thread pool as follows:

1) First we submitted task to the thread pool, when the core number of threads that are not useful, it will start one of the core thread to perform tasks, remember to illustrate this process, taking up the task of our time is very long, so a short time will not end;

2) the second third and then submit the task to the thread pool, the same logic to perform their first task is exactly the same, will start the core thread pool thread pool thread to perform the remaining two tasks your new submission.

3) Then there are the new job submission over, this time found the thread core thread pool thread pool are already at work, it will look at whether taskQueue task queue is full, and did not find, is empty, so this when the task into the task queue for the core thread pool idle threads themselves have to take the task execution.

4) Then he submitted four tasks to the thread pool, they were judged core thread is idle, not idle, and then determine whether the task queue is full, dissatisfaction, directly into the task queue;

5) followed by a new task again , then after judging the core thread pool and the task queue, the task is still found no way to deal with will determine whether the maximum number of threads and found that no, the new thread is started to perform the task;

6) Next came a task, perform the same procedure 5);

7) One more task, find the core of the thread pool is busy, the task queue was full, all the threads in the pool are also at work, there is no way to deal with him, so he found a saturation strategy, because strategy is the default saturation throw abnormal, so the thread pool thread will tell submit the task has been no thread can use the.

More than just a core number of threads is 3, the total number of threads is 5, the task queue length is 5, default policy adoption process Throws strategy from the beginning to the end of the thread pool operate at full capacity

More java learning materials may be concerned about: itheimaGZ get

Published 735 original articles · won praise 3 · Views 110,000 +

Guess you like

Origin blog.csdn.net/u010395024/article/details/104834511