I need to block threads on ForkJoinPool when its queue is full. This can be done in the standard ThreadPoolExecutor, e.g.:
private static ExecutorService newFixedThreadPoolWithQueueSize(int nThreads, int queueSize) {
return new ThreadPoolExecutor(nThreads, nThreads,
5000L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(queueSize, true), new ThreadPoolExecutor.CallerRunsPolicy());
}
I know, there is some Dequeue inside ForkJoinPool, but I don't have access to it via its API.
Update: Please see the answer below.
After some research I am happy to answer the question:
Reason: There is no such option in the ForkJoinPool's implementation due to the following reason. The majority of j.u.c. Executors assume single concurrent queue and many threads. This leads to the queue contention and degrades performance when reading/writing to the queue by multiple threads. Thus, such an approach is not quite scalable --> High contention on the queue can generate a large number of context switches and CPU-business.
Implementation: In the ForkJoinPool each thread has a separate double-ended queue (Deque) backed by an array. To minimize contention, Work-stealing happens at the tail of the deque, whereas task-submission happens at the head by current thread (worker). The tail contains the largest portion of work. In other words, stealing from the tail by another worker-thread minimizes the number of times to interact with other workers --> less contention, better overall performance.
The idea is described in the official white paper "Java Fork/Join Framework" by Doug Lea.
Scalability benchmarks are shown in "Let it crash - Scalability of Fork Join Pool"
Work-around thoughts: There's are global submission queues. Submissions from non-FJ threads enter into submission queues (Workers take these tasks). There are also Worker-queues mentioned above.
Maximum size for the queues is limited by the number:
When the queue is full an unchecked exception is thrown:
This is described in javadocs.
(Also, see ThreadPool's constructor for
UncaughtExceptionHandler
)I tend to claim that current implementation doesn't have such a mechanism and this should be implemented in the consuming API by us.
For example, this could be done as follows:
ForkJoinPool.getQueuedSubmissionCount()
).Here's the official JSR-166E java code of ForkJoinPool for more information.