简体   繁体   中英

Multi Threading Java ScriptEngine

I have a script engine that is created for each thread. The problem is that each thread waits for the other threads to complete. The threads should be be running async. When I comment out where the ScriptEngine.eval() line the code runs just like is should

Start the Threads there are about 57 threads created every time.

for (CalculationThread calcThread : this.calcThreads) {
    calcThread.start();
}

A script manager and script engine is created for each thread. The script engine is solving a equation that is 43.0*0.76282-0.154. This is a very simple equation.

ScriptEngineManager mgr = new ScriptEngineManager();
        for (ScriptEngineFactory sef : mgr.getEngineFactories()) {
            if (sef.getParameter("THREADING") == null) {
                System.out.println("this is not thread safe: " + this.threadName);
            } else {
                System.out.println("This is thread safe: " + this.threadName);
            }
        }
        ScriptEngine engine = mgr.getEngineByName("js");

        String modText = this.equationCalculation.getEquation();

        for (int i = this.counter; i < this.counter + this.equationCalculation.getTileSize(); i++) {

            String tempModText = "";

            boolean noData = false;
            boolean cannot = false;

            tempModText = modText;

            for (int j = 1; j < this.equationCalculation.getImages().size(); j++) {
//              code that does stuff in the loop
            }



            //Code that does other stuff

                    try {
                        Number theNumber = (Number) engine.eval(tempModText);
                        this.equationCalculation.setOutputAtIndex(0,i,theNumber.floatValue());
                    } catch (ScriptException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        this.equationCalculation.setOutputAtIndex(0,i,0);
                    }
            }
        }

My question is do I have the script engine implemented wrong? When I comment out where the script engine evals the string it takes 20 sec to run throw 54 million pixels but when a leave the script engine in it takes 21 mins.

Another question, is the script engine just to slow for what I am wanting it to do?

Please do not leave a comment that says way are you using a script engine to solve that equation.

The problem here is that you are using a scriptengine. Evaluating the string takes a while and i think you are doing this for every pixel. Try to not to evaluate the script every time you use it. There are possibilities to precompile your script. Have a look at this post: https://stackoverflow.com/questions/12220267/with-java-scriptengine-groovy-how-to-make-it-more-performant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM