我有一个 EJB 来计算某事。只要计算运行,精度就会提高。所以一个 async 函数开始计算,一个 async 应该停止它。但是在 Glassfish 3.1.2 调用上运行stopCalculating()
并不会创建一个新线程,而是等到startCalculating()
完成,这显然永远不会发生。
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Singleton;
@Singleton
public class Calculator {
private boolean calculating = false;
private String result = "Empty";
@Asynchronous
public void startCalculating() {
calculating = true;
Logger.getGlobal().log(Level.INFO, "Starting!");
calculate();
}
private void calculate() {
result = "";
while(calculating) {
/*Calculate and update result*/
Logger.getGlobal().log(Level.INFO, "Calculate...");
}
}
@Asynchronous
public Future<String> stopCalculating() {
Logger.getGlobal().log(Level.INFO, "Stopping!");
calculating = false;
return new AsyncResult<String>(result);
}
}
如何让GlassfishstopCalculating()
在其他线程中运行?