Java Reference
In-Depth Information
subCount += right . join ();
}
return
return subCount ;
}
}
public
public static
void main ( String [] args ) {
d = createArrayOfRandomDoubles ();
int
static void
int n = new
new ForkJoinTask ( 0 , 9999999 ));
System . out . println ( "Found " + n + " values" );
new ForkJoinPool (). invoke ( new
}
}
The fork() and join() methods here are the key: we'd be hard-pressed to implement this
sort of recursion without those methods (which are not available in the tasks executed by a
ThreadPoolExecutor ). Those methods use a series of internal, per-thread queues to manipu-
late the tasks and switch threads from executing one task to executing another. The details of
that are transparent to the developer, though if you're interested in algorithms, the code
makes fascinating reading. Our focus here is on the performance: what trade-offs are there
between the ForkJoinPool and ThreadPoolExecutor classes?
First and foremost is that the suspension implemented by the fork/join paradigm allows all
the tasks to be executed by only a few threads. Counting the double values in an array of 10
million elements using this example code creates more than 2 million tasks, but those tasks
are easily executed by only a few threads (even one, if that makes sense for the machine run-
ning the test). Running a similar algorithm using a ThreadPoolExecutor would require
more than 2 million threads, since each thread would have to wait for its subtasks to com-
plete, and those subtasks could only complete if there were additional threads available in the
pool. So the fork/join suspension allows us to use algorithms that we otherwise could not,
which is a performance win.
However, while divide-and-conquer techniques are very powerful, overusing them can yield
worse performance. In the counting example, a single thread can scan and count the array,
though that won't necessarily be as fast as running the fork/join algorithm in parallel.
However, it would be easy enough to partition the array into chunks and use a
ThreadPoolExecutor to have multiple threads scan the array:
public
public class
class ThreadPoolTest
ThreadPoolTest {
private
private double
double [] d ;
Search WWH ::




Custom Search