2015-05-02 8 views
0

我编写了下面的代码,我的问题是我想将数据从FICSR类传递到主类。我尝试在FICSR类的构造函数中声明“在calcFICS()方法内提到的ArrayList”,但之后我发现我无法在 的calcFICS()方法中使用它们,为什么?以及如何解决它。如何在分离类中的线程将数据传递给主类

MainClass

public static void main(String[] args) { 
    MatFactory matFactory = new MatFactory(); 
    FilePathUtils.addInputPath(path_Obj); 
    Mat bgrMat = matFactory.newMat(FilePathUtils.getInputFileFullPathList().get(0)); 

    if (bgrMat != null) { 
     if (!bgrMat.empty()) { 
      fiCSR = new FICSR(bgrMat, SysConsts.MIN_CS_RADIUS); 
     } else { 
      Log.E(TAG, "MainClass", "bgrMat is empty"); 
     } 
    } else { 
     Log.E(TAG, "MainClass", "bgrMat is null"); 
    } 

FICSR类

public FICSR(Mat bgrMat, int csRadius) { 
    // TODO Auto-generated constructor stub 
    this.bgrMat = bgrMat; 
    this.csRadius = csRadius; 

    this.fiCS_R3 = new Thread(new FICS(this.bgrMat, this.csRadius), "FICS_R" + this.csRadius); 
    fiCS_R3.start(); 
} 

    private class FICS implements Runnable { 

    private Mat bgrMat; 
    private int csRadius; 

    public FICS(Mat bgrMat, int csRadius) { 
     // TODO Auto-generated constructor stub 
     this.bgrMat = bgrMat; 
     this.csRadius = csRadius; 
    } 

    public void run() { 
     // TODO Auto-generated method stub 
     calcFICS(this.bgrMat, this.csRadius); 
    } 

    public static void calcFICS(Mat bgrMat, int csRadius) { 
    // TODO Auto-generated method stub 

    ArrayList<Mat> onOffCSActRegMatsList = null; 
    ArrayList<Mat> offOnCSActRegMatsList = null; 

    ArrayList<Mat> onOffCSFullMatsList = null; 
    ArrayList<Mat> offOnCSFullMatsList = null; 

    onOffCSActRegMatsList = new ArrayList<Mat>(); 
    offOnCSActRegMatsList = new ArrayList<Mat>(); 

    onOffCSFullMatsList = new ArrayList<Mat>(); 
    offOnCSFullMatsList = new ArrayList<Mat>(); 

    //here I want to add values to the ArrayLists defined above and return them to the main class. how can I do that? 

... ... ...

+0

这两个类在同一个包中? – AADProgramming

+0

@AADTechnical是的......如果他们在同一个cpackage中,那可能会有帮助吗? – user2121

回答

0

拥有一流水平的变量,用干将和制定者,不断追加值和它在米ain方法使用getter来检索值。

+0

你的意思是在FICS类或FICSR类中,因为我试图在FICS类中声明列表,并且它们在calcFICS()方法内部不是accessibe – user2121

1

您无法访问它,因为calcFICS是静态的,并且您无法从静态方法访问非静态字段。 但是,如果要在工作线程中进行计算以利用所有CPU核心,则更好的解决方案是使用ExecutorService,例如,一个ForkJoinPool。

FICS类:

class FICS { 
    static class Result { 
    ArrayList<Mat> onOffCSActRegMatsList; 
    ArrayList<Mat> offOnCSActRegMatsList; 
    ArrayList<Mat> onOffCSFullMatsList; 
    ArrayList<Mat> offOnCSFullMatsList; 
    } 

    public static Result calcFICS(Mat bgrMat, int csRadius) { 
    Result result = new Result(); 
    result.onOffCSActRegMatsList = new ArrayList<Mat>(); 
    result.offOnCSActRegMatsList = new ArrayList<Mat>(); 

    result.onOffCSFullMatsList = new ArrayList<Mat>(); 
    result.offOnCSFullMatsList = new ArrayList<Mat>(); 
    //add values to the ArrayLists 
    return result; 
    } 
} 

主类:直到计算结束

... 
ForkJoinPool pool = new ForkJoinPool(); 
Future<FICS.Result> fut = pool.submit(() -> FICS.calcFICS(bgrMat, SysConsts.MIN_CS_RADIUS)); 
FICS.Result result = fut.get(); 
... 

fut.get()将阻止主线程。但在调用任何get方法之前,您可以向池中提交几个计算。