Swing的后台线程
问题描述:
我不知所措,简单的使用swing工作人员。 我在doInBackground()中添加了一些简单的代码,但它没有执行,我没有收到异常。当我使用debuger时,他正在按照应该的方式工作。 )) 可能有人有这样的事情,或告诉我想法如何缓存这个错误,或... 对不起,代码是复杂的。告诉我你需要更多的东西或评论。 如果我删除“installer.setFPS(fPSCalculatorGhost.getFPS());” - 字符串,一切都会好起来的。Swing的后台线程
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
public MainWindow() {
initComponents();
}
private void initComponents() {
InterfaceUpdateWorker interfaceUpdate = new InterfaceUpdateWorker(
new InterfaceInfoInstallerImpl());
interfaceUpdate.setCamera(gLEventListenerImpl.getCameraGhost());
interfaceUpdate.setfPSCalculatorGhost(gLEventListenerImpl.getFPSCalculatorGhost());
interfaceUpdate.execute();
}
@Override
protected Void doInBackground() throws InterruptedException {
while(true) {
installer.setFPS(fPSCalculatorGhost.getFPS());
installer.setCameraXPosition(
cameraGhost.getCameraXPosition());
installer.setCameraYPosition(
cameraGhost.getCameraYPosition());
installer.setCameraZPosition(
cameraGhost.getCameraZPosition());
Thread.sleep(200);
}
}
public final class FPSCalculatorGhost {
private FPSCalculatorGhost() {
}
public float getFPS() {
return fpsTask.getAvrfps();
}
}
public float getAvrfps() {
synchronized (this) {
return avrfps;
}
}
一切都围绕着fpsTask对象。它由interfaceUpdate-thread(或应用程序工作线程)使用,并由其他线程使用,并在其中进行初始化。结果: 1)。 fpsTask对象在一个线程中初始化为 2)。 fpsTask对象将值赋给另一个线程。
当我从FPSCalculatorGhost最终制作fpsTask时,它开始工作。
答
所以,问题是installer.setFPS(fPSCalculatorGhost.getFPS());
线。它有什么作用? 它调用getAvrFPS
方法,这包含此块:
synchronized (this) {
return avrfps;
}
这块时同时没有其他线程处于一些同步块或相同的对象的同步方法只能输入。在你发布的代码片段中没有这样的块/方法,所以你必须自己搜索它。
最重要的是,确保持有锁的其他线程不会等待此工作线程的某些结果。
当您遇到死锁时,运行jstack
并使用java进程的进程ID来获取所有正在运行的线程的堆栈跟踪 - 这包括它们正在等待的锁。 (jps
为您提供了所有java进程ID。)
你能发表一些代码吗? – 2011-03-28 00:51:34
与罗曼一致 - 如果您隐藏我们的代码,我们如何能够猜测您的错误? – 2011-03-28 01:06:16
我不明白当我做fpsTask最后,它开始工作 – itun 2011-03-28 02:12:37