![](/img/trans.png)
[英]Is this the correct way to implement multithreading on Server side of concurrent client/server setup?
[英]GWT client side multithreading revisited
我将一个Swing应用程序转换为GWT,该应用程序大量使用了多线程。 该代码在后台运行连续的数值模拟,并在每帧中以图形方式显示结果。 仿真需要知道每次计算要经过多少实时时间。
Thread calculate = new Thread(new Runnable(){
// run calculation cycles every 10 ms
// (or as fast as possible)
long prevT=System.currentTimeMillis();
while(!isFinished){
long currT = System.currentTimeMillis(),
dt = currT-prevT;
if(dt<10) Thread.sleep(5);
else{
simulate(dt);
prevT = currT;
}
}
}
// Update UI every 60 ms
java.awt.Timer timer = new Timer(60, new ActionListener(){public void actionPerformed(ActionEvent e){
results = getCurrentSimState();
uiPanel.render(results);
}});
此外,还有来自用户的事件驱动代码,这些代码可更改模拟器状态。
在GWT中实现此目标的最佳方法是什么? 以前的大多数多线程问题主要与异步客户端-服务器工作有关。 我看到了GWT(客户端)中的线程问题,并且想知道Scheduler
是否合适:
void onLoad(){
AnimationScheduler.get().requestAnimationFrame(callback, uiElement);
}
AnimationCallback cb = new AnimationCallback(){public void execute(double t){
results = getCurrentSimState();
render(results, uiElement);
AnimationScheduler.get().requestAnimationFrame(callback, uiElement);
}};
double prevT=Duration.currentTimeMillis();
Scheduler.get().scheduleIncremental(new RepeatingCommand(){ public void execute() {
double currT = Duration.currentTimeMillis(),
dt = currT-prevT;
if(dt>30){
calculate(dt);
prevT = currT;
}
return isFinished;
}});
由于我是GWT的新手,我真的很想知道是否
RepeatingCommand
计算,还是在AnimationScheduler
进行GUI更新? 您可能要考虑使用Web Workers进行真正的后台处理,而不是使用Scheduler“模拟”多线程。
请注意,使用Scheduler中的Incremental命令,必须在工作完成时返回“ false”,而在需要继续进行时返回“ true”。 仅当您可以将计算分割成小块,然后在可以执行用户操作的块之间将控制权交还给JavaScript时,此命令才有效。
如果您不能将工作分成小块,则只有在每个计算周期所花费的时间少于两次计算之间的时间间隔时,才能使用Scheduler方法工作,从而使JavaScript有机会对两次计算之间的用户活动做出反应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.