The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

Jdk7 - Fork Join Framework


Basically the Fork-Join breaks the task at hand into mini-tasks until the mini-task is simple enough that it can be solved without further breakups.

It’s like a divide-and-conquer algorithm.

One important concept to note in this framework is that ideally no worker thread is idle. They implement a work-stealing algorithm in that idle workers the work from those workers who are busy.

It’s based on the work of Doug Lea, a thought leader on Java concurrency. Fork/Join deals with the threading hassles; you just indicate to the framework which portions of the work can be broken apart and handled recursively. It employs pseudocode (as taken from Doug Lea’s paper on the subject):


Result solve(Problem problem) {
if (problem is small)
directly solve problem
else {
split problem into independent parts
fork new subtasks to solve each part
join all subtasks
compose result from subresults
}
}


Core Classes used in Fork/Join Framework
ForkJoinPool.
ForkJoinTask.
The ForkJoinPool is basically a specialized implementation of ExecutorService implementing the work-stealing algorithm we talked about above. We create an instance of ForkJoinPool by providing the target parallelism level i.e. the number of processors as shown below:
ForkJoinPool pool = new
ForkJoinPool(numberOfProcessors);
Where numberOfProcessors =
Runtime.getRunTime().availableProcessors();


The ForkJoinTask  class provides several methods for checking the execution status of a task.
The isDone() method returns true if a task completes in any way. The isCompletedNormally() method returns true if a task completes without cancellation or encountering an exception, and isCancelled() returns true if the task was cancelled.
Lastly,isCompletedabnormally() returns true if the task was either cancelled or encountered an exception.
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com