-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingAction.java
44 lines (36 loc) · 990 Bytes
/
PingAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
import java.util.concurrent.*;
/**
* This class illustrates how to create a ForkJoinTask that does not return
* a result.
* @author camel
*/
public class PingAction extends RecursiveAction {
private ArrayList<Target> arrayList;
private static final int THRESHOLD = 4;
private int start;
private int end;
private int tm;
public PingAction(ArrayList<Target> array, int start, int end, int timeout) {
this.arrayList = array;
this.start = start;
this.end = end;
this.tm = timeout;
}
protected void compute() {
if (end - start < THRESHOLD) {
computeDirectly();
} else {
int middle = (end + start) / 2;
PingAction subTask1 = new PingAction(arrayList, start, middle, tm);
PingAction subTask2 = new PingAction(arrayList, middle, end, tm);
invokeAll(subTask1, subTask2);
}
}
protected void computeDirectly() {
for (int i = start; i < end; i++) {
OneAction one = new OneAction(arrayList.get(i), tm);
one.exec();
}
}
}