ThreadPoolExecutor javadoc,排队和锁定的三种策略
ThreadPoolExecutor javadoc, three strategies for queuing and lockups
在 ThreadPoolExecutor class 的 Oracle 文档中写道:
There are three general strategies for queuing:
Direct handoffs. A good default choice for a work queue is a SynchronousQueue that hands
off tasks to threads without otherwise holding them. Here, an attempt
to queue a task will fail if no threads are immediately available to
run it, so a new thread will be constructed. This policy avoids
lockups when handling sets of requests that might have internal
dependencies. Direct handoffs generally require unbounded
maximumPoolSizes to avoid rejection of new submitted tasks. This in
turn admits the possibility of unbounded thread growth when commands
continue to arrive on average faster than they can be processed.
Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new
tasks to wait in the queue when all corePoolSize threads are busy.
Thus, no more than corePoolSize threads will ever be created. (And the
value of the maximumPoolSize therefore doesn't have any effect.) This
may be appropriate when each task is completely independent of others,
so tasks cannot affect each others execution; for example, in a web
page server. While this style of queuing can be useful in smoothing
out transient bursts of requests, it admits the possibility of
unbounded work queue growth when commands continue to arrive on
average faster than they can be processed.
...
为什么直接切换策略比无界队列策略更能避免死锁?还是我理解有误?
假设您有 corePoolSize = 1
。如果第一个任务将另一个任务提交到同一个池并等待结果,它将无限期锁定。
但是,如果任务完全独立,就没有理由使用直接切换来防止锁定。
这只是一个示例,internal dependency
可以表示很多不同的意思。
在 ThreadPoolExecutor class 的 Oracle 文档中写道:
There are three general strategies for queuing:
Direct handoffs. A good default choice for a work queue is a SynchronousQueue that hands off tasks to threads without otherwise holding them. Here, an attempt to queue a task will fail if no threads are immediately available to run it, so a new thread will be constructed. This policy avoids lockups when handling sets of requests that might have internal dependencies. Direct handoffs generally require unbounded maximumPoolSizes to avoid rejection of new submitted tasks. This in turn admits the possibility of unbounded thread growth when commands continue to arrive on average faster than they can be processed.
Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created. (And the value of the maximumPoolSize therefore doesn't have any effect.) This may be appropriate when each task is completely independent of others, so tasks cannot affect each others execution; for example, in a web page server. While this style of queuing can be useful in smoothing out transient bursts of requests, it admits the possibility of unbounded work queue growth when commands continue to arrive on average faster than they can be processed. ...
为什么直接切换策略比无界队列策略更能避免死锁?还是我理解有误?
假设您有 corePoolSize = 1
。如果第一个任务将另一个任务提交到同一个池并等待结果,它将无限期锁定。
但是,如果任务完全独立,就没有理由使用直接切换来防止锁定。
这只是一个示例,internal dependency
可以表示很多不同的意思。