문제

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.

도움이 되었습니까?

해결책

So, the problem is the installer.setFPS(fPSCalculatorGhost.getFPS()); line. What does it do? It calls the getAvrFPS method, and this contains this block:

synchronized (this) {
   return avrfps;
}

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.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top