2012-03-12 59 views
0

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

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

回答

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

非常感谢您! – lochi 2012-03-13 01:56:54

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; 
    } 
} 
1

你需要一个Callable r而不是一个RunnableCallable返回你的结果。

如下你可以得到它:

Future<YourExpectedResult> future = executor.submit(new YourCallable()); 
YourExpectedResult result = future.get();