Fork-Join池没有产生任何结果,而串行代码工作最佳



我写的其他串行代码工作完美,但这个并行版本没有给出任何输出,它只是打印hello,我正在用"hey"测试它。它从来没有到达那一行,也就是说,它卡住了调用方法,我在网上找不到任何有用的资源。

请帮助我了解我在哪里错了,我是新的并行编程。

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

y[1] =中位数[2 2 80]= 2

y[2] =中位数[2 80 6]=中位数[2 6 80]= 6

y[3] =中位数[80 6 3]=中位数[3 6 80]= 6

y[4] =中位数[6 3 3]=中位数[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块为空。

同样在你的构造函数中,你有this.arrayFiltered = arrayFiltered;,它实际上什么也不做,因为你没有名称arrayFiltered参数。导致arrayFiltered不能初始化,导致arrayFiltered.add(i, inputArray.get(i));发生NPE

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

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

是错误的。因为这里有两行。你需要读一行,记住结果,然后测试它并对它进行分析。

这样的

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

相关内容

  • 没有找到相关文章

最新更新