如何在固定的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;
    }
}

最佳答案

使用Callable:

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();

07-24 22:33