0

In my project, I have a method which loads a big model from local disk. Loading the model takes about 15 minutes and sometimes more. What i'm thinking to do is to create a runnable method which loads the model for once and then, from different classes i call this method to execute some code. in fact, i'm not sure how to achieve that, could you please guide me? Here is simple pseudo code:

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

in my mind i'd like to create something similar to server but not server:)

Updated:The code below is what I've tried so far.

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

The Second class:

public class B{

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

I have to prolem with the above code: 1. it stops running once it has loaded the model. 2. I can't invoke any method from the frist class.

nick zoum
  • 7,216
  • 7
  • 36
  • 80

1 Answers1

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

}
nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • but i want A class running until i manually stop it.During running, it supposes to execute the request and return the result back.I tried to do something similar but i couldn't send any parameter to A .Another problem is that Class A stops running after loading the file . – Abood Al-mars Dec 02 '16 at 17:29
  • @AboodAl-mars So are you trying to make two classes communicate from different java applications? – nick zoum Dec 03 '16 at 08:39
  • Then you might want to look into [inter process communication](http://stackoverflow.com/questions/10942427/how-to-have-2-jvms-talk-to-one-another). I had made [something similar](http://stackoverflow.com/questions/39804975/java-how-can-i-create-an-application-that-will-only-start-if-an-instance-of-it) a while ago, i think it will help you. – nick zoum Dec 03 '16 at 10:15
  • Yes .i think this hard problem.i implemented the code you mentioned,but when i call the method get similarity from different classe, the get load method load the model again – Abood Al-mars Dec 04 '16 at 10:20