ThreadPoolExecutor source notes

Basic strategy:

1. When submitting a new task, as long as the number of threads less than the core number of threads, it will immediately create a new thread to him, even if the existing thread not working.

2. If the pool where more than core threads of threads, after the length of time they KeepAliveTime does not work, will be stopped.

3. When the number of threads than the core, the priority request to be queued.

4. If a request can not be placed in the queue, will generally prefer to create a thread, unless exceed the maximum number of threads.

Three queues approach:

1. If you can not put into the queue, add a direct thread. This did not limit the default maximum number of threads. When the first thread has dependencies, this strategy will not be locked.

2. unbounded queue. In this case, there will be no more than the number of core threads. For there is no dependency relationship between when a thread

3. bounded queue. Helps reduce resource exhaustion. Big queue small pool will reduce the cpu usage, thereby reducing throughput. Small queue large pool will increase the cpu usage, and scheduling overhead, thereby reducing throughput.

Handle four kinds of strategies:

1. Default RejectedExecutionException direct throw an exception.

2. Let the thread calling execute its own processing tasks

3. can not perform tasks directly lost.

4. The front of the queue, the most direct lost, before attempting to perform.

5. can define your own strategy, but requires special care.

Continued ================= ===============

Published 42 original articles · won praise 0 · Views 1654

Guess you like

Origin blog.csdn.net/weixin_42504835/article/details/104641293