2012-03-12 76 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();