1

我很茫然,一个简单的swing worker的使用。我在 doInBackground() 中添加了一些简单的代码,但它没有执行,我没有收到异常。当我使用调试器时,他正在正常工作。))可能有人有这样的事情,或者告诉我如何缓存这个错误的想法,或者......对不起,代码很复杂。告诉我你需要更多的东西或评论。如果我删除“installer.setFPS(fPSCalculatorGhost.getFPS());”-string,一切都会好起来的。

    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-object。它由 interfaceUpdate-thread(或应用程序工作线程)使用,并由初始化它的其他线程使用。结果:1)。fpsTask-object 在一个线程中初始化 2)。fpsTask-object 给另一个线程赋值。

当我从 FPSCalculatorGhost 最终制作 fpsTask 时,它开始工作。

4

1 回答 1

1

所以,问题出installer.setFPS(fPSCalculatorGhost.getFPS());在线路上。它有什么作用?它调用该getAvrFPS方法,其中包含以下块:

synchronized (this) {
   return avrfps;
}

synchronized只有当没有其他线程同时处于同一对象的某个同步块或同步方法中时,才能进入该块。在您发布的代码片段中,没有这样的块/方法,因此您必须自己搜索。

最重要的是,确保持有锁的其他线程没有等待该工作线程的某些结果。

当您遇到死锁时,jstack使用您的 java 进程的进程 ID 运行以获取所有正在运行的线程的堆栈跟踪 - 这包括它们持有和等待的锁。(jps为您提供所有 Java 进程 ID。)

于 2011-03-28T01:16:46.723 回答