Swing 中的后台线程
我很茫然,摇摆工人的简单使用。 我在 doInBackground() 中添加了一些简单的代码,但它没有执行,我没有收到异常。当我使用调试器时,他正在正常工作。 )) 可能有人有类似的事情,或者告诉我如何缓存这个错误的想法,或者...... 抱歉,代码很复杂。告诉我您是否需要更多内容或评论。 如果我删除“installer.setFPS(fPSCalculatorGhost.getFPS());”字符串,一切都会好起来的。
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 对象。它由接口更新线程(或应用程序工作线程)使用,并由初始化的其他线程使用。 结果: 1). fpsTask-object 在一个线程中初始化 2)。 fpsTask-object 向另一个线程提供值。
当我从 FPSCalculatorGhost 最终制作 fpsTask 时,它开始工作。
I'm at a loss, a simple use of swing worker.
I added a few simple code in doInBackground(), but it does not execute, I don`t receive exceptions. When I use debuger, He is working as it should. ))
May be somebody has something like this, or make tell me ideas how to cache this mistake, or ...
Sorry, code is complex. Tell me is you need something more or comments.
if I remove "installer.setFPS(fPSCalculatorGhost.getFPS());"-string, everything will be alright.
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;
}
}
Everything revolves around fpsTask-object. It is used by interfaceUpdate-thread (or application worker thread) and it is used by other thread, where it is initialized.
Outcome:
1). fpsTask-object is initialized in one thread
2). fpsTask-object gives values to another thread.
When I make fpsTask from FPSCalculatorGhost final, it begin to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,问题出在
installer.setFPS(fPSCalculatorGhost.getFPS());
行。它有什么作用?它调用 getAvrFPS 方法,其中包含此块:
只有当没有其他线程同时处于某个同步块或同步方法中时,才能进入此同步块。同一个对象。在您发布的代码片段中没有这样的块/方法,因此您必须自己搜索。
最重要的是,确保持有锁的其他线程没有等待该工作线程的某些结果。
当您遇到死锁时,请使用 java 进程的进程 ID 运行 jstack 来获取所有正在运行的线程的堆栈跟踪 - 这包括它们持有和等待的锁。 (
jps
为您提供所有 java 进程 ID。)So, the problem is the
installer.setFPS(fPSCalculatorGhost.getFPS());
line. What does it do?It calls the
getAvrFPS
method, and this contains this block:This
synchronized
block can only be entered when no other thread at the same time is in some synchronized block or synchronized method of the same object. In the code snippet you posted there is no such block/method, so you have to search it yourself.Most importantly, make sure the other thread who holds the lock is not waiting on some result from this worker thread.
When you run into the deadlock, run
jstack
with the process-ID of your java process to get stack traces of all threads running - this includes the locks they are holding and waiting on. (jps
gives you all java process IDs.)