Note that there are some explanatory texts on larger screens.

plurals
  1. POHow is the fork/join framework better than a thread pool?
    primarykey
    data
    text
    <p>What are the benefits of using the new <a href="http://download.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html" rel="noreferrer">fork/join framework</a> over just simply splitting the big task into N subtasks in the beginning, sending them to a cached thread pool (from <a href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html" rel="noreferrer">Executors</a>) and waiting for each task to complete? I fail to see how using the fork/join abstraction simplifies the problem or makes the solution more efficient from what we've had for years now.</p> <p>For example, the parallelized blurring algorithm in the <a href="http://download.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html" rel="noreferrer">tutorial example</a> could be implemented like this:</p> <pre><code>public class Blur implements Runnable { private int[] mSource; private int mStart; private int mLength; private int[] mDestination; private int mBlurWidth = 15; // Processing window size, should be odd. public ForkBlur(int[] src, int start, int length, int[] dst) { mSource = src; mStart = start; mLength = length; mDestination = dst; } public void run() { computeDirectly(); } protected void computeDirectly() { // As in the example, omitted for brevity } } </code></pre> <p>Split in the beginning and send tasks to a thread pool:</p> <pre><code>// source image pixels are in src // destination image pixels are in dst // threadPool is a (cached) thread pool int maxSize = 100000; // analogous to F-J's "sThreshold" List&lt;Future&gt; futures = new ArrayList&lt;Future&gt;(); // Send stuff to thread pool: for (int i = 0; i &lt; src.length; i+= maxSize) { int size = Math.min(maxSize, src.length - i); ForkBlur task = new ForkBlur(src, i, size, dst); Future f = threadPool.submit(task); futures.add(f); } // Wait for all sent tasks to complete: for (Future future : futures) { future.get(); } // Done! </code></pre> <p>The tasks go to the thread pool's queue, from which they're executed as worker threads become available. As long as the splitting is granular enough (to avoid having to particularly wait for the last task) and the thread pool has enough (at least N of processors) threads, all processors are working at full speed until the whole computation is done.</p> <p>Am I missing something? What's the added value of using the fork/join framework?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload