【问题标题】:How can a thread in a separat class passes data to the main class单独类中的线程如何将数据传递给主类
【发布时间】:2015-05-02 12:50:39
【问题描述】:

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

主类

    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");
    }

FISR 类

    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?

... ... ...

【问题讨论】:

  • 这两个类在同一个包中吗?
  • @AADTechnical 是的...如果它们在同一个 cpackage 中,会有所帮助吗??

标签: java multithreading arraylist


【解决方案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 方法之前将多个计算提交到池中。

【讨论】:

    【解决方案2】:

    有一个类级别的变量,带有 getter 和 setter,不断将值附加到它,并在 main 方法中使用 getter 来检索值。

    【讨论】:

    • 你的意思是在 FICS 类还是 FICSR 类中,因为我试图在 FICS 类中声明列表,而它们在 calcFICS() 方法中是不可访问的
    猜你喜欢
    • 1970-01-01
    • 2013-05-31
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多