2016-11-01 113 views
0

我一直在閱讀有關這一點,並從這些文章中,我得到我可能是一個錯誤SwingWorker的執行所完成的()完成doInBackground()之前

SwingWorker, done() is executed before process() calls are finished

The proper way to handle exceptions thrown by the SwingWorker.doInBackground

但是在我情況下,沒有什麼可以調用done方法。這是我的工人的工作方式。

public static class collectingWorker extends SwingWorker<Void, Void> { 


    collectingWorker(String name) { 
     //initialize 
     threadName = name; 
    } 

    @Override 
    public Void doInBackground() {  

     loop = true; 
     while(loop){ 
      //Code goes here 
     } 
     return null; 
    } 

    @Override 
    protected void done() { 
     System.out.println("loop: " + loop); 
     System.out.println("Collecting worker DONE"); 
     try { 
      get(); 
     } catch (CancellationException x) { 
      System.out.println("Cancelation"); 
      // ... 
     } catch (InterruptedException x) { 
      System.out.println("Interruption"); 
      // ... 
     } catch (ExecutionException x) { 
      System.out.println("Execution"); 
      System.out.println(x.getMessage()); 
      System.out.println(x.getCause()); 
      // ... 
     } 
    } 
} 

而在另一個線程上,我有一個計數器在將循環設置爲false前等待x分鐘。我所看到的是,CollectWorker done方法是在它應該等待的x分鐘之前執行的,而且更糟糕的是,它完全是隨機的,有時可以工作,有時會在3分鐘後失敗,有時會在90分鐘後失敗。

這是我在done方法從打印件的,你可以看到,布爾「循環」是永遠不會設置爲false

loop: true 
Collecting worker DONE 
Execution 
java.util.NoSuchElementException 
java.util.NoSuchElementException 

任何意見或解決方法?

回答

0

嵌套在while(循環)中的邏輯拋出一個NoSuchElementException,導致整個doInBackground()方法過早退出。

相關問題