import
java.io.*;
import
java.util.concurrent.ForkJoinPool;
import
java.util.concurrent.RecursiveTask;
class
SearchTask
extends
RecursiveTask<Integer> {
int
array[];
int
start, end;
int
searchElement;
public
SearchTask(
int
array[],
int
start,
int
end,
int
searchElement)
{
this
.array = array;
this
.start = start;
this
.end = end;
this
.searchElement = searchElement;
}
protected
Integer compute()
{
return
processSearch();
}
private
Integer processSearch()
{
int
count =
0
;
for
(
int
i = start; i <= end; i++) {
if
(array[i] == searchElement) {
count++;
}
}
return
count;
}
}
public
class
GFG {
public
static
void
main(String args[])
{
int
array[] = {
1
,
2
,
6
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
6
};
int
searchElement =
6
;
int
start =
0
;
int
end = array.length -
1
;
ForkJoinPool pool = ForkJoinPool.commonPool();
SearchTask task =
new
SearchTask(array, start, end,
searchElement);
int
result = pool.invoke(task);
System.out.println(searchElement +
" found "
+ result +
" times "
);
}
}