简体   繁体   English

多线程Java ScriptEngine

[英]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 当我注释掉ScriptEngine.eval()行代码运行时应该是什么样的

Start the Threads there are about 57 threads created every time. 启动线程每次创建大约57个线程。

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. 脚本引擎正在求解方程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. 当我评论脚本引擎在哪里篡改字符串时,需要20秒才能运行5400万像素,但是当它离开脚本引擎时需要21分钟。

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. 这里的问题是你使用的是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 看看这篇文章: https//stackoverflow.com/questions/12220267/with-java-scriptengine-groovy-how-to-make-it-more-performant

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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