[英]Multi Threading Java ScriptEngine
我有一个为每个线程创建的脚本引擎。 问题是每个线程都在等待其他线程完成。 线程应该是异步运行。 当我注释掉ScriptEngine.eval()行代码运行时应该是什么样的
启动线程每次创建大约57个线程。
for (CalculationThread calcThread : this.calcThreads) {
calcThread.start();
}
为每个线程创建脚本管理器和脚本引擎。 脚本引擎正在求解方程43.0 * 0.76282-0.154。 这是一个非常简单的等式。
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);
}
}
}
我的问题是我的脚本引擎实现错了吗? 当我评论脚本引擎在哪里篡改字符串时,需要20秒才能运行5400万像素,但是当它离开脚本引擎时需要21分钟。
另一个问题是,脚本引擎只是为了减慢我想要它做的事情?
请不要留下一条评论,说明您使用脚本引擎来解决这个问题。
这里的问题是你使用的是scriptengine。 评估字符串需要一段时间,我认为你是为每个像素做这个。 每次使用时都尽量不要评估脚本。 有可能预编译您的脚本。 看看这篇文章: https : //stackoverflow.com/questions/12220267/with-java-scriptengine-groovy-how-to-make-it-more-performant
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.