An Introduction to the Fork / Join Framework
In the previous posts about the java.util.concurrent framework, I introduced what for me is the core of the package and what you will use the most on real-world applications.
Going further with the topic, it’s worth to know the new fork/join framework introduced in Java 7. The pros and cons of this framework over the usage of thread pools or regular thread/runnable development has been subject of a lot of debate, but I think it is still a nice tool to have in your arsenal.
I’ll use a more classic approach to show you the basic structure of the fork/join framework and code a version of the Quicksort algorithm using it.
My goal here is to introduce you to the tools provided by the fork/join structure in Java 7, this is not a discussion about the best Quicksort implementation. If you want to delve into implementing the best Quicksort possible I recommend you check out this great post about it.
My goal here is to introduce you to the tools provided by the fork/join structure in Java 7, this is not a discussion about the best Quicksort implementation. If you want to delve into implementing the best Quicksort possible I recommend you check out this great post about it.
The fork/join framework provides a very straightforward and intuitive structure to implement recursive and divide and conquer problems that can be solved concurrently. You start with the big problem and break it down in smaller work units until each work unit can be solved directly.
The implementation of your problem solver must be a subclass of ForkJoinTask, and you will most likely implement a subclass of it’s children RecursiveAction or RecursiveTask; RecursiveAction represents a recursive problem solver, capable of solving a small enough problem directly or breaking it down into smaller work units; RecursiveTask is similar, but it also has the capability of returning a result for each work unit executed.
To implement our Quicksort we will use the in-place strategy (we are hardcore, right?) and use a generic list as data so it supports any Java Comparable. With these premises our task can be coded as:
01.
package
com.ricardozuasti;
02.
03.
import
java.util.List;
04.
import
java.util.concurrent.RecursiveAction;
05.
06.
public
class
QuickSortextends
Comparable>
extends
RecursiveAction {
07.
08.
private
List data;
09.
private
int
left;
10.
private
int
right;
11.
12.
public
QuickSort(List data){
13.
this
.data=data;
14.
this
.left =
0
;
15.
this
.right = data.size() -
1
;
16.
}
17.
18.
public
QuickSort(List data,
int
left,
int
right){
19.
this
.data = data;
20.
this
.left = left;
21.
this
.right = right;
22.
}
23.
24.
@Override
25.
protected
void
compute() {
26.
if
(left < right){
27.
int
pivotIndex = left + ((right - left)/
2
);
28.
29.
pivotIndex = partition(pivotIndex);
30.
31.
invokeAll(
new
QuickSort(data, left, pivotIndex-
1
),
32.
new
QuickSort(data, pivotIndex+
1
, right));
33.
}
34.
35.
}
36.
37.
private
int
partition(
int
pivotIndex){
38.
T pivotValue = data.get(pivotIndex);
39.
40.
swap(pivotIndex, right);
41.
42.
int
storeIndex = left;
43.
for
(
int
i=left; i
44.
if
(data.get(i).compareTo(pivotValue) <
0
){
45.
swap(i, storeIndex);
46.
storeIndex++;
47.
}
48.
}
49.
50.
swap(storeIndex, right);
51.
52.
return
storeIndex;
53.
}
54.
55.
private
void
swap(
int
i,
int
j){
56.
if
(i != j){
57.
T iValue = data.get(i);
58.
59.
data.set(i, data.get(j));
60.
data.set(j, iValue);
61.
}
62.
}
63.
}
The compute() method is the one that the fork/join framework will invoke for each work unit. Our base step (small work unit) is to do nothing (since a size 1 list is always sorted). Our recursive step uses the invokeAll() method, which is a shortcut to fork() all small work units and then join() them all again.
And that’s it! We don’t really need anything else. To actually use our new concurrent Quicksort we use a ForkJoinPool instance:
01.
package
com.ricardozuasti;
02.
03.
import
java.util.ArrayList;
04.
import
java.util.List;
05.
import
java.util.concurrent.ForkJoinPool;
06.
07.
public
class
Concurrency3 {
08.
public
static
void
main(String[] args) {
09.
final
int
SIZE =
10000
;
10.
11.
List myList =
new
ArrayList(SIZE);
12.
13.
for
(
int
i=
0
; i
14.
int
value = (
int
) (Math.random() *
100
);
15.
myList.add(value);
16.
}
17.
18.
QuickSort quickSort =
new
QuickSort(myList);
19.
20.
ForkJoinPool pool =
new
ForkJoinPool();
21.
pool.invoke(quickSort);
22.
}
23.
}
And again the invoke method is equivalent to doing a fork() followed by a join(), so after thepool.invoke() method returns our list is sorted.
No comments:
Post a Comment