2016-12-02 54 views
0

在我的项目中,我有一种从本地磁盘加载大型模型的方法。加载模型大约需要15分钟,有时更多。我想要做的是创建一个runnable方法加载模型一次,然后,从不同的类我调用此方法来执行一些代码。事实上,我不知道如何实现这一点,请你指导我吗? 下面是简单的伪代码:调用不同类的可运行方法

// class A has two method , load the model , and does some calculation 
Class A: 1.Runnable method: LoadModel(); 
      2.Mehtod2: distance(); 
// here i would like to run this programe anytime, pass some parameters and call the method "distance" in class A 
Class B: 1.import Loadmodel() class and invoke distance(); 

在我的脑海,我想创建一个类似于服务器而不是服务器的东西:)

更新:下面的代码是我到目前为止已经试过。

public class load implements Runnable { 

    WordVectors wordVectors; 

    public void run() { 
     try { 
      load(); 
     } catch (FileNotFoundException ex) { 
      java.util.logging.Logger.getLogger(load.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (UnsupportedEncodingException ex) { 
      java.util.logging.Logger.getLogger(load.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 

    public void load() throws FileNotFoundException, UnsupportedEncodingException { 
     //Your display method implementation. 
      wordVectors = WordVectorSerializer.loadTxtVectors(new File("glove.twitter.27B.200d.txt")); 
    } 

    public double Simmiraty(String a, String b){ 
     return wordVectors.similarity(a,b); 
    } 

    public static void main(String[] args) { 
     load Obj= new load(); 
     Obj.run(); 
    } 
} 

第二类:

public class B{ 

    public static void main(String[] args) throws Exception { 
     load ob =new load(); 
     System.out.println(ob.Simmiraty("iphone", "battery")); 
    } 
} 

我有上面的代码prolem: 1.一旦加载模型停止运行。 2.我无法从第一类调用任何方法。

+0

您的代码不可读。请使用stackoverflow代码块功能。 – SachinSarawgi

回答

0
public class Load implements Runnable{ 

    private InputStream stream; 
    private static final Load instance; 
    private WordVectors wordVectors; 

    static { 
     instance = new Load(); 
     instance.run(); 
    } 

    public static Load GetLoad(){ 
     return instance; 
    } 

    private Load(){ 
     if(instance != null) 
      throw new UnsupportedOperationException(); 
    } 

    public voir run() { 
     if(wordVectors != null) 
      throw new UnsupportedOperationException(); 
     try { 
      load(); 
     } catch (Exception ex) { 
      Logger.getLogger(load.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    public void load() throws FileNotFoundException, UnsupportedEncodingException { 
     stream = new InputStream(new File("glove.twitter.27B.200d.txt")); 
     wordVectors = WordVectorSerializer.loadTxtVectors(stream,false); 
    } 

    public void interrupt(){ 
     if(stream != null) 
      stream.close(); 
    } 

    public double Simmiraty(String a, String b){ 
     return wordVectors.similarity(a,b); 
    } 

    public static void main(){ 
     Load load = GetLoad(); 
    } 

} 

public class B{ 

    public void function(){ 
     Load load = Load.GetLoad(); 
    } 

} 
+0

但我想要一个类运行,直到我手动停止它。在运行时,它假设执行请求并返回结果back.I试图做类似的事情,但我不能发送任何参数给A。另一个问题是该类A在加载文件后停止运行。 –

+0

@ AboodAl-mars所以你想让两个类从不同的Java应用程序进行通信? –

+0

然后你可能想看看[进程间通信](http://stackoverflow.com/questions/10942427/how-to-have-2-jvms-talk-to-one-another)。我做了[类似的东西](http://stackoverflow.com/questions/39804975/java-how-can-i-create-an-application-that-will-only-start-if-an-instance-of-它)不久前,我认为它会帮助你。 –

相关问题