2016-11-01 115 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()方法过早退出。

相关问题