Searching...
Tuesday, 24 June 2014

Java 7 Fork and Join Example

01:40
Now a days parallel computing has become necessary for heavy or complex operations. Java 7 has introduced a Fork and Join framework which distributes the work across multiple cores and join them after completion which is the final result. These tasks or work is divided into small tasks until these task dont need any divide and all these tasks carried out separately. These task use a pool and task are divided according to that. The core classes which will be useful for fork and join are the ForkJoinPool and ForkJoinTask
Here is one simple example to print numbers based on the input size
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

/**
 *
 * This class helps to understand the feature of ForkJoinPool and ForkJoinTasks
 *
 * ForkJoinTask is an abstract class ,RecursiveAction and RecursiveTasks are the subclasses of ForkJoinTask class
 *
 * In case of return type required we need to use RecursiveTask
 *
 * In this example we used RecursiveAction and override its call() method to implement fork and join
 *
 * This is an example to print 1 to the given number
 *
 * The given number is less then 100 it will not split the task (fork)
 *
 * If the given number is 100 or greater than 100 it will split it in to two task (1 to 50 and 51 to 100)
 *
 * Finally join each task
 *
 *@authorvinod
 *
 */
public class ForkAndJoinExample {

public static void main(String[] args) {

ForkJoinPool fjpool = new ForkJoinPool(1);
RecursiveAction task = new PrintJob(100);
fjpool.invoke(task);

}
}

class PrintJob extends RecursiveAction {
private static final long serialVersionUID = 1L;
private int lines = 0;

public PrintJob(int lines) {
this.lines = lines;
}

@Override
protected void compute() {
if (lines < 100) {
print(1, lines, "single task");
} else {
PrintJob p1 = new PrintJob(lines);
p1.print(1, lines / 2, "task 1");
PrintJob p2 = new PrintJob(lines);
p2.print(lines / 2, lines, "task 2");
p1.fork();
p2.fork();
p1.join();
p2.join();
}
}

private void print(int start, int end, String taskname) {
for (int i = start; i <= end; i++) {
System.out.println("print job triggerd from " + taskname + " " + i);

}

}

}
Output
print job triggerd from task 1  31
print job triggerd from task 1 32
print job triggerd from task 1 33
print job triggerd from task 1 34
print job triggerd from task 1 35
print job triggerd from task 1 36
print job triggerd from task 1 37
print job triggerd from task 1 38
print job triggerd from task 1 39
print job triggerd from task 1 40
print job triggerd from task 1 41
print job triggerd from task 1 42
print job triggerd from task 1 43
print job triggerd from task 1 44
print job triggerd from task 1 45
print job triggerd from task 1 46
print job triggerd from task 1 47
print job triggerd from task 1 48
print job triggerd from task 1 49
print job triggerd from task 1 50
print job triggerd from task 2 50
print job triggerd from task 2 51
print job triggerd from task 2 52
print job triggerd from task 2 53
print job triggerd from task 2 54
print job triggerd from task 2 55
print job triggerd from task 2 56
print job triggerd from task 2 57
print job triggerd from task 2 58
print job triggerd from task 2 59
print job triggerd from task 2 60
print job triggerd from task 2 61
print job triggerd from task 2 62
print job triggerd from task 2 63
print job triggerd from task 2 64
print job triggerd from task 2 65
print job triggerd from task 2 66
print job triggerd from task 2 67
print job triggerd from task 2 68
print job triggerd from task 2 69
print job triggerd from task 2 70
print job triggerd from task 2 71
print job triggerd from task 2 72
print job triggerd from task 2 73
print job triggerd from task 2 74
print job triggerd from task 2 75
print job triggerd from task 2 76
print job triggerd from task 2 77
print job triggerd from task 2 78
print job triggerd from task 2 79
print job triggerd from task 2 80
print job triggerd from task 2 81
print job triggerd from task 2 82
print job triggerd from task 2 83
print job triggerd from task 2 84
print job triggerd from task 2 85
print job triggerd from task 2 86
print job triggerd from task 2 87
print job triggerd from task 2 88
print job triggerd from task 2 89
print job triggerd from task 2 90
print job triggerd from task 2 91
print job triggerd from task 2 92
print job triggerd from task 2 93
print job triggerd from task 2 94
print job triggerd from task 2 95
print job triggerd from task 2 96
print job triggerd from task 2 97
print job triggerd from task 2 98
print job triggerd from task 2 99
print job triggerd from task 2 100
print job triggerd from task 1 1
print job triggerd from task 1 2
print job triggerd from task 1 3
print job triggerd from task 1 4
print job triggerd from task 1 5
print job triggerd from task 1 6
print job triggerd from task 1 7
print job triggerd from task 1 8
print job triggerd from task 1 9
print job triggerd from task 1 10
print job triggerd from task 1 11
print job triggerd from task 1 12
print job triggerd from task 1 13
print job triggerd from task 1 14
print job triggerd from task 1 15
print job triggerd from task 1 16
print job triggerd from task 1 17
print job triggerd from task 1 18
print job triggerd from task 1 19
print job triggerd from task 1 20
print job triggerd from task 1 21
print job triggerd from task 1 22
print job triggerd from task 1 23
print job triggerd from task 1 24
print job triggerd from task 1 25
print job triggerd from task 1 26
print job triggerd from task 1 27
print job triggerd from task 1 28
print job triggerd from task 1 29
print job triggerd from task 1 30
print job triggerd from task 1 31
print job triggerd from task 1 32
print job triggerd from task 1 33
print job triggerd from task 1 34
print job triggerd from task 1 35
print job triggerd from task 1 36
print job triggerd from task 1 37
print job triggerd from task 1 38
print job triggerd from task 1 39
print job triggerd from task 1 40
print job triggerd from task 1 41
print job triggerd from task 1 42
print job triggerd from task 1 43
print job triggerd from task 1 44
print job triggerd from task 1 45
print job triggerd from task 1 46
print job triggerd from task 1 47
print job triggerd from task 1 48
print job triggerd from task 1 49
print job triggerd from task 1 50

0 comments: