Queues available to the java thread pool

Constructor of java thread pool ThreadPoolExecutor:

You can see that the queue used by the thread pool is the blocking queue BlockingQueue.

Which queue to use depends on the queuing policy to be used. Generally speaking, thread pool has three queuing strategies: direct submission, unbounded queue and bounded queue.

Direct submission policy

The default option for work queues is synchronous queue, which submits tasks directly to threads without holding them. Here, if there is no thread available to run the task immediately, the attempt to queue the task will fail, so a new thread will be constructed. This policy avoids locks when processing request sets that may have internal dependencies. Direct submission usually requires unbounded maximumpoolsizes to avoid rejecting newly submitted tasks. This strategy allows unbounded threads to grow when commands arrive continuously more than the average number that the queue can handle.

Synchronousqueue is unbounded, that is, its ability to save data tasks is unlimited. However, due to the characteristics of the queue itself, you must wait for other threads to remove elements before adding them.

Unbounded queue

Using unbounded queues (for example, linkedblockingqueue without predefined capacity) will cause new tasks to wait in the queue when all corepoolsize threads are busy. In this way, the threads created will not exceed corepoolsize. (therefore, the value of maximumpoolsize is invalid.) when each task is completely independent of other tasks, that is, task execution does not affect each other, it is suitable to use unbounded queue; for example, in web page server. This queue can be used to process transient burst requests. This policy allows unbounded threads to grow when commands arrive continuously more than the average number that the queue can handle The possibility of.

Linkedblockingqueue is a BlockingQueue with variable size. If the size is specified during its construction, the generated BlockingQueue has a size limit. If the size is not specified, its size has integer MAX_ Value. The objects contained are sorted in FIFO order.

Priorityblockingqueue is similar to linkedblockingqueue, but the sorting of its objects is not FIFO, but determined according to the natural order of the objects or the comparator of the constructor.

Bounded queue

When using limited maximumpoolsizes, Bounded queues, such as arrayblockingqueue, help prevent resource exhaustion, but may be difficult to adjust and control. Queue size and maximum pool size may need to compromise each other: using large queues and small pools can minimize CPU utilization, operating system resources and context switching overhead, but may lead to manual throughput reduction. If tasks are blocked frequently (for example, if they are I / O boundaries), the system may schedule more threads than you allow. Using small queues usually requires a large pool size and high CPU utilization, but may encounter unacceptable scheduling overhead, which will also reduce throughput.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>