在我的项目中,我有一种从本地磁盘加载大型模型的方法。加载模型大约需要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.我无法从第一类调用任何方法。
您的代码不可读。请使用stackoverflow代码块功能。 – SachinSarawgi