我有一个为每个线程创建的脚本引擎。问题是每个线程都在等待其他线程完成。线程应该正在运行异步。当我注释掉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分钟。
另一个问题是,脚本引擎只是为了减缓我想要它做的事情吗?
请不要留下这样的评论:你用脚本引擎来解决这个问题。
发布于 2015-12-23 13:46:07
这里的问题是你使用的是脚本。计算字符串需要一段时间,我认为每个像素都是这样做的。每次使用脚本时,尽量不要对其进行评估。可以预编译脚本。看看这篇文章:https://stackoverflow.com/questions/12220267/with-java-scriptengine-groovy-how-to-make-it-more-performant
https://stackoverflow.com/questions/34420657
复制相似问题