SwingWorker的done()没有被执行

时间:2017-03-27 15:09:08

标签: java swing swingworker

在doInBackground()完成后没有执行done()方法。

我有以下代码:

public class collectingWorker_USB4Java_Async_Fast extends SwingWorker<Void, Void> {     

    collectingWorker_USB4Java_Async_Fast(String name) {
        //initialize
        collectingWorkerDone = false;
    }

    @Override
    public Void doInBackground() {    

        loop = true;  

        while(loop){  
            collectData();                
            try {
                synchronized(TransferCallbackData) {
                    TransferCallbackData.wait();
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(GraphPanel_JChart2D.class.getName()).log(Level.SEVERE, null, ex);
            }       
        }
        System.out.println("Out of the loop of collecting worker, loop: " + loop);
        return null;
    }  


    @Override
    protected void done() {
        collectingWorkerDone = true;
        System.out.println("Collecting worker DONE"); 
    }
}   

但由于某种原因,在doInBackground()完成后它不会被执行。

我有一个while循环的工人。为了完成它我只是将布尔(循环)设置为false。

public void StartPlotting(){      
    Graphs.createCollectingThread();
    Graphs.collectWorker_usb4java_async_fast.execute();
}        

public void StopPlotting(){ 
    if (Graphs.collectWorker_usb4java_async_fast != null){
        if (Graphs.collectWorker_usb4java_async_fast.getState() == SwingWorker.StateValue.STARTED){
            Graphs.collectWorker_usb4java_async_fast.loop = false;
        }
        //wait until plotting is done
        synchronized(Graphs.collectWorker_usb4java_async_fast.TransferCallbackData){
            Graphs.collectWorker_usb4java_async_fast.TransferCallbackData.notify();
        }
        while(Graphs.collectWorker_usb4java_async_fast.getState() != SwingWorker.StateValue.DONE){
            synchronized(Graphs.collectWorker_usb4java_async_fast.TransferCallbackData){
                Graphs.collectWorker_usb4java_async_fast.TransferCallbackData.notify();
            }
        }  
    }
}  

public void StopPlottingWithCheck(){  
    if (Graphs.collectWorker_usb4java_async_fast != null){
        if (Graphs.collectWorker_usb4java_async_fast.getState() == SwingWorker.StateValue.STARTED){
            Graphs.collectWorker_usb4java_async_fast.loop = false;
        }
        //wait until plotting is done
        synchronized(Graphs.collectWorker_usb4java_async_fast.TransferCallbackData){
            Graphs.collectWorker_usb4java_async_fast.TransferCallbackData.notify();
        }
        while(Graphs.collectWorker_usb4java_async_fast.getState() != SwingWorker.StateValue.DONE){
            synchronized(Graphs.collectWorker_usb4java_async_fast.TransferCallbackData){
                Graphs.collectWorker_usb4java_async_fast.TransferCallbackData.notify();
            }
        }  
    }
    while(Graphs.collectingWorkerDone != true){
        //wait unil transfers dont receive more data
    }
}

如果像这样调用StopPlotting(),我得到:

Collecting worker DONE

但是,如果调用StopPlottingWithCheck(),则代码会停止。

在调试模式下,我可以看到一旦StopPlotting()完成就会执行done()方法,这会让我认为我有EDT同步问题或类似问题。

有没有办法确保在doInBackground()完成后立即执行done()方法?

0 个答案:

没有答案