简体   繁体   English

Fork-Join Pool没有产生任何结果,而串行代码则可以最佳地工作

[英]Fork-Join Pool not producing any result whereas the serial codes works optimally

The other serial code I wrote works perfectly, but this parallel version is not giving any output, it just prints hello, I am testing it with "hey" it never even gets to that line ie it gets stuck on invoke method, I couldn't find any helpful resources online. 我编写的其他串行代码运行完美,但是此并行版本没有提供任何输出,它只会打印问候,我正在用“嘿”测试它,甚至从未到达该行,即卡在invoke方法上,我无法找不到在线有用的资源。

Please help me in understanding where I am going wrong I am new to parallel programming. 请帮助我了解我要去哪里错了,我是并行编程的新手。

The code is a median filter program, that when given an array x=[2,80,6,3] the filtered array is y=[2,6,6,3] calculated as: 该代码是一个中值滤波程序,当给定数组x = [2,80,6,3]时,滤波后的数组y = [2,6,6,3]的计算公式为:

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

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

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

y[4] = Median[6 3 3] = Median[3 3 6] = 3 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){}

 }



  }

If you type "hey" when run this program you'll get parsing exception which is ignored because of empty catch block. 如果在运行该程序时键入“ hey”,则会得到解析异常,由于catch块为空,该异常被忽略。

Also in your constructor you have this.arrayFiltered = arrayFiltered; 同样在您的构造函数中,您有this.arrayFiltered = arrayFiltered; which is actually do nothing because you have no parameter with name arrayFiltered. 实际上没有任何作用,因为您没有名称为arrayFiltered的参数。 It lead to arrayFiltered will not be initialized which lead to NPE in arrayFiltered.add(i, inputArray.get(i)); 它导致arrayFiltered不会被初始化,从而导致arrayFiltered.add(i, inputArray.get(i)); NPE arrayFiltered.add(i, inputArray.get(i));

I suggest yo to put e.printStackTrace(); 我建议哟把e.printStackTrace(); in catch block it will immediately show all errors. catch块中,它将立即显示所有错误。 And of course you need to learn how to use debugger. 当然,您需要学习如何使用调试器。

Also this part 还有这部分

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

is wrong. 是错的。 because you read two line here. 因为您在这里读了两行。 You need to read line once, remember result then test it and pars it. 您需要读取一次行,记住结果然后对其进行测试并进行解析。

Like this 像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM