gpt4 book ai didi

java - Fork-Join Pool 不产生任何结果,而串行代码工作最佳

转载 作者:行者123 更新时间:2023-11-30 08:01:01 26 4
gpt4 key购买 nike

我编写的其他串行代码工作得很好,但是这个并行版本没有给出任何输出,它只是打印你好,我用“嘿”测试它,它甚至从未到达该行,即它卡在调用方法上,我在网上找不到任何有用的资源。

请帮助我理解我哪里出了问题,我是并行编程的新手。

该代码是一个中值过滤程序,当给定数组 x=[2,80,6,3] 时,过滤后的数组为 y=[2,6,6,3],计算如下:

y[1] = Median[2 2 80] = 2

y[2] = Median[2 80 6] = Median[2 6 80] = 6

y[3] = Median[80 6 3] = Median[3 6 80] = 6

y[4] = Median[6 3 3] = Median[3 3 6] = 3

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.io.PrintWriter;

public class ParallelFilter extends RecursiveAction {

static final int SEQUENTIAL_THRESHOLD=500;
ArrayList<Float> inputArray;
int sizeOfFilter;
ArrayList<Float> outputlist;
ArrayList<Float> arrayFiltered;

float high;
float low;

public ParallelFilter(ArrayList<Float> inputArray,int sizeOfFilter,float high,float low)
{

this.inputArray=inputArray;
this.outputlist=outputlist;
this.sizeOfFilter=sizeOfFilter;
this.arrayFiltered=arrayFiltered;
//this.index=index;
this.high=high;
this.low=low;
}




protected void compute()
{
int index=1;

if((high - low) < SEQUENTIAL_THRESHOLD )
{
while(inputArray.size() > sizeOfFilter){
for(int i=0; i<sizeOfFilter;i++){

arrayFiltered.add(i,inputArray.get(i));

}

Collections.sort(arrayFiltered);
float median = arrayFiltered.get(arrayFiltered.size()/2);
outputlist.add(index,median);
inputArray.remove(inputArray.get(0));
arrayFiltered.clear();
index=index+1;

}

outputlist.add(inputArray.get(inputArray.size()-1));
}


else{

ParallelFilter leftTask = new ParallelFilter(inputArray,sizeOfFilter,low,(low+high)/2);
ParallelFilter rightTask = new ParallelFilter(inputArray,sizeOfFilter,(low+high)/2,high);
leftTask.fork();
rightTask.compute();
leftTask.join();
}

}



public static void main(String[] args){

try
{
Scanner sc;
sc = new Scanner(new File("inp1.txt"));
sc.useDelimiter(" ");
ArrayList<Float> inputlist = new ArrayList<>();

//POPULATE LIST FROM FILE
while (sc.hasNextLine()) {

if(sc.nextLine().length() == 1){continue;}
String[] parts = sc.nextLine().split(" "); // split each line by " "

inputlist.add(Float.parseFloat(parts[1])) ;
// System.out.println(parts[1]);
}

System.out.println("Enter filter size : ");
Scanner in = new Scanner(System.in);
int sizeOfFilter = in.nextInt();
if (sizeOfFilter < 3 || sizeOfFilter / 2 == 0) {
System.out.println("Filter size should be odd and bigger than 3");
}

float low = inputlist.get(0);
float high = inputlist.get(inputlist.size()-1);

ParallelFilter pf = new ParallelFilter(inputlist,sizeOfFilter,low,high);
System.out.println("hello");
ForkJoinPool forkJoinPool = new ForkJoinPool();
forkJoinPool.invoke(pf);//suspect the problem is here...

for (int i=0; i<pf.outputlist.size();i++){

System.out.println(pf.outputlist.get(i));}
System.out.println("hey");

}
catch(Exception e){}

}



}

最佳答案

如果您在运行此程序时输入“hey”,您将收到解析异常,该异常由于空的 catch block 而被忽略。

此外,在您的构造函数中,您还有 this.arrayFiltered = arrayFiltered; ,它实际上什么也不做,因为您没有名称为 arrayFiltered 的参数。它导致arrayFiltered不会被初始化,从而导致arrayFiltered.add(i, inputArray.get(i));

中出现NPE

我建议您将 e.printStackTrace(); 放在 catch block 中,它会立即显示所有错误。当然,您需要学习如何使用调试器。

还有这部分

            if (sc.nextLine().length() == 1) {
continue;
}
String[] parts = sc.nextLine().split(" ");

是错误的。因为你在这里读了两行。您需要读取一次行,记住结果然后测试它并解析它。

像这样

            String line = sc.nextLine();
if (line.length() == 1) {
continue;
}
String[] parts = line.split(" ");

关于java - Fork-Join Pool 不产生任何结果,而串行代码工作最佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com