我尝试通过拆分它来检查元素是否在字符串中:
`public class ParallelSearchComment扩展了RecursiveTask { private static final long serialVersionUID = 1L;
int lo;
int hi;
String com;
String query;
int T;
ParallelSearchComment(String c, int l, int h, String q, int Treshold){
com=c;
lo=l;
hi=h;
query=q;
T=Treshold;
}
private int findMiddle(String text){ // Only split at whitespace chars (or some words would be missed)
int middle = lo + ((hi - lo)/2);
int i= middle;
for (i = middle; i != hi && com.charAt(middle) != ' ' && i-middle <= query.length()+1; i++){
}
middle = i;
return middle;
}
@Override
protected Boolean compute() {
int middle = findMiddle(com);
if (hi - lo < T || hi-middle <= query.length()) {//hi-middle <= query.length()
return com.substring(lo, hi).contains(query);
} else {
ParallelSearchComment left = new ParallelSearchComment(com, lo, middle, query, T);
ParallelSearchComment right = new ParallelSearchComment(com, middle, hi, query, T);
left.fork();
boolean resRight = right.compute();
boolean resLeft = left.join();
return resRight || resLeft;
}
}
static boolean ParallelSearchComment(String c, String query, int T,int p) {
final ForkJoinPool fjPool = new ForkJoinPool(p);
ParallelSearchComment t = new ParallelSearchComment(c, 0, c.length(), query, T);
return fjPool.invoke(t);
}
`
答案 0 :(得分:1)
问题是,每次调用ForkJoinPool
时,您都要创建一个新的ParallelSearchComment
实例。相反,您应该创建一个ForkJoinPool
并在整个应用程序中使用。
答案 1 :(得分:0)
由于您的代码段未显示如何调用static boolean ParallelSearchComment
,因此我假设它已用于初始化您的搜索。另外,我假设您仅从main
触发了一次搜索。
这样,您传入的并行度值或p
很可能高于当前JVM堆可以处理的值。看看当你把它减少到5或10时会发生什么。