Java Reference
In-Depth Information
Fork/Joinuses work stealing tominimizethreadcontentionandoverhead.Eachwork-
er thread from a pool of worker threads has its own double-ended work queue and
pushesnewtaskstothisqueue.Itreadsthetaskfromtheheadofthequeue.Ifthequeue
is empty, the worker thread tries to get a task from the tail of another queue. Stealing
is infrequent because worker threads put tasks into their queues in a last-in, first-out
(LIFO)order,andthesizeofworkitemsgetssmallerasaproblemisdividedintosub-
problems. You start by giving the tasks to a central worker and it keeps dividing them
intosmallertasks.Eventuallyalltheworkershavesomethingtodowithminimalsyn-
chronization.
Fork/Join largely consists of the java.util.concurrent package's
ForkJoinPool , ForkJoinTask , ForkJoinWorkerThread , RecursiveAc-
tion , and RecursiveTask classes:
ForkJoinPool is an ExecutorService implementation for running
ForkJoinTask s. A ForkJoinPool instance provides the entry point for
submissionsfromnon- ForkJoinTask clients,aswellasprovidingmanage-
ment and monitoring operations.
ForkJoinTask is the abstract base class for tasks that run within a
ForkJoinPool context. A ForkJoinTask instance isathread-like entity
that is much lighter weight than a normal thread. Huge numbers of tasks
and subtasks may be hosted by a small number of actual threads in a
ForkJoinPool , at the price of some usage limitations.
ForkJoinWorkerThread describes a thread managed by a
ForkJoinPool instance, which executes ForkJoinTask s.
RecursiveAction describes a recursive resultless ForkJoinTask .
RecursiveTask describes a recursive result-bearing ForkJoinTask .
The Java documentation provides examples of RecursiveAction -based tasks
(such as sorting) and RecursiveTask -based tasks (such as computing Fibonacci
numbers).Youcanalsouse RecursiveAction toaccomplishmatrixmultiplication
(see http://en.wikipedia.org/wiki/Matrix_multiplication ).
Forexample, supposethat you'vecreated Listing 6-7 ' s Matrix class torepresent a
matrix consisting of a specific number of rows and columns.
Listing 6-7. A class for representing a two-dimensional table
class Matrix
{
private double[][] matrix;
Search WWH ::




Custom Search