Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java's fork-and-join thread pool is good for executing IO bound task?

Tags:

java

fork-join

in my application, I have to solve a problem by executing many network-io bound task and sometime one io bound task and be divided into smaller io bound tasks. These tasks are currently getting executed using Java's standard threadpool mechanism. I am wondering whether I can move to fork-and-join framework? But the question is, is forkandjoin framework usually being used to solve io bound operations or CPU bound? I assume they are mostly for CPU bound operations cause fork-and-join framework makes use of work stealing technique to make use of multo core processors, but if I use it for IO bound tasks, will there be any adverse effect?

like image 933
Shamik Avatar asked Nov 21 '11 01:11

Shamik


People also ask

When would you use a fork join pool vs normal thread pool?

1) The main difference between ForkJoinPool and ThreadPoolExecutor is that ForkJoinPool is designed to accept and execute ForkJoinTask, which is a lightweight version of FutureTask, while ThreadPoolExecutor is designed to provide a normal thread pool which executes each submitted task using one of possibly several ...

What is a fork join thread pool?

ForkJoinPool It is an implementation of the ExecutorService that manages worker threads and provides us with tools to get information about the thread pool state and performance. Worker threads can execute only one task at a time, but the ForkJoinPool doesn't create a separate thread for every single subtask.

What is difference between ExecutorService and ForkJoinPool?

The Fork/Join framework in Java 7 is an implementation of the Divide and Conquer algorithm, in which a central ForkJoinPool executes branching ForkJoinTasks. ExecutorService is an Executor that provides methods to manage the progress-tracking and termination of asynchronous tasks.

What is the advantage of thread pool in Java?

Advantages of a Thread PoolBetter performance. Saves time. No need to create a thread again and again. Easy to access.


3 Answers

Fork-join is designed for compute-bound tasks so generally I'd say no. Fork-join does have an API (the ManagedBlocker api) to tell the FJ framework that your thread will be blocking for a while and not to line up new tasks but it's really designed for short waits (like obtaining a lock), not arbitrarily long waits for IO.

We have a system that uses fork-join and we shunt IO-bound tasks off to a separate executor pool. When data arrives it triggers tasks into the fork-join pool so that only cpu-bound work occurs there.

like image 179
Alex Miller Avatar answered Sep 28 '22 09:09

Alex Miller


If you are trying to address the "I/O bound" aspect of your problem, I doubt that switching from standard threads to fork-and-join is going to improve things ... assuming that you've implemented the current thread-based solution properly. (And based on Alex Miller's answer, the switch could actually make things significantly worse.)

Or to put it another way, the way to make your I/O bound application go faster is to address the problems that make it I/O bound ... or increase your system's I/O bandwidth.

like image 41
Stephen C Avatar answered Sep 28 '22 10:09

Stephen C


there does not seem to be a compelling advantage to fork-joins in this case.

there does not seem to be a signficant disadvantage either because you would not be driving some resource too hard.

all in all, i would stay with the thread pool until you have no other important development to do.

like image 32
necromancer Avatar answered Sep 28 '22 10:09

necromancer