0

如何访问固定 threadExecutor 中的 getOptimizedMol() 方法?当每个优化器线程完成时,我需要获得返回分子吗?

import chemaxon.marvin.calculations.ConformerPlugin;
import chemaxon.marvin.plugin.PluginException;
import chemaxon.struc.Molecule;

public class Optimizer3D implements Runnable {


    private Molecule mol3D;
    private Molecule mol2D;

    Optimizer3D(Molecule mol2D_)
    {
       this.mol2D = mol2D_; 
    }

    @Override
    public void run() {
        mol3D = chemAxonOptimizer();
    }

    private Molecule chemAxonOptimizer()
    {
        //optimize molecule code not shown
        return molecule;

    }


    public Molecule getOptimizedMol()
    {
        return mol3D;
    }
}
4

3 回答 3

1

一种简单的技术是将某种 JobListener 添加到您的可运行文件中:

public class Optimizer3D implements Runnable {

    public static interface JobListener {
         public void onJobFinished(Optimizer3D job);
    }

    private Molecule mol3D;
    private Molecule mol2D;
    private JobListener jobListener;
    Optimizer3D(Molecule mol2D_, JobListener listener)
    {
       this.mol2D = mol2D_; 
       this.jobListener = listener;
    }

    @Override
    public void run() {
        mol3D = chemAxonOptimizer();
        if (jobListener!=null) {
            jobListener.onJobFinished(this);
        }
    }

    private Molecule chemAxonOptimizer()
    {
        //optimize molecule code not shown
        return molecule;

    }


    public Molecule getOptimizedMol()
    {
        return mol3D;
    }
}
于 2012-03-12T21:25:33.703 回答
1

你需要 aCallable而不是 a RunnableCallable返回您的结果。

您可以按以下方式获取它:

Future<YourExpectedResult> future = executor.submit(new YourCallable());
YourExpectedResult result = future.get();
于 2012-03-12T21:27:22.600 回答
0

使用可调用:

public class Optimizer3D implements Callable<Molecule>{
    private Molecule mol3D;
    private Molecule mol2D;

    Optimizer3D(Molecule mol2D_)
    {
       this.mol2D = mol2D_; 
    }

    @Override
    public Molecule call() {
        mol3D = chemAxonOptimizer();
        return getOptimizedMol();
    }

    private Molecule chemAxonOptimizer()
    {
        //optimize molecule code not shown
        return molecule;

    }


    private Molecule getOptimizedMol()
    {
        return mol3D;
    }
}

然后从你的执行人那里:

Future<Molecule > future = executor.submit(yourTask);
Molecule  yourValue = future.get();
于 2012-03-12T21:27:16.877 回答