如何在不冻结GUI的情况下调用SwingWorker .get()?

时间:2012-11-16 21:42:49

标签: java swing swingworker

我做了一个样本来澄清问题:

public class Worker extends SwingWorker<Integer, Integer> {
    private GeneralUserInterface gui;

    public Worker(GeneralUserInterface gui){
        this.gui = gui;
    }

    @Override
    protected Integer doInBackground() throws Exception {
        int someResultToReturn = 10;

        for(int i=0; i<100; i++){
            Thread.sleep(50);//The Work
            publish(i+1);//calls process, which updates GUI
        }

        return someResultToReturn;
    }

    @Override
    protected void process(List<Integer> values) {
        for (Integer val : values) {
            gui.updateProgressBar(val);
        }
    }
}

private void jButtonDoWorkActionPerformed(java.awt.event.ActionEvent evt) {
    Worker worker = new Worker(this);
    worker.execute();

    try {
        int resultToGet = worker.get();//Obviously freezes the GUI
    } catch (InterruptedException | ExecutionException ex) {}

    //NEXT LINE NEEDS THE RESULT TO CONTINUE
}

public void updateProgressBar(int value){
    this.jProgressBar1.setValue(value);
}

正如您所猜测的那样,对worker.get()的调用会使UI无响应,这是正常的,因为它等待线程完成。这种问题通常是如何解决的?

2 个答案:

答案 0 :(得分:6)

  

这种问题通常如何解决?

通常,您所做的是覆盖Swingworker.done()方法。完成后台线程后,在GUI线程中执行完成。然后你可以安全地调用get而不用阻塞,并做任何你需要做的事情。

以下是一种方法:

public class Worker extends SwingWorker<Integer, Integer> {
    private GeneralUserInterface gui;

    // omitted...

    @Override
    protected Integer doInBackground() throws Exception {
        int someResultToReturn = 10;

        for(int i=0; i<100; i++){
            Thread.sleep(50);//The Work
            publish(i+1);//calls process, which updates GUI
        }

        return someResultToReturn;
    }

    // omitted...

    @Override
    protected void done() {

        try {
           int resultToGet = worker.get();//Obviously freezes the GUI
        } catch (InterruptedException | ExecutionException ex) {}

       //NEXT LINE NEEDS THE RESULT TO CONTINUE
   }
}

private void jButtonDoWorkActionPerformed(java.awt.event.ActionEvent evt) {
    Worker worker = new Worker(this);
    worker.execute();
}

然而,这可能不是最方便的设计。我发现最好让GUI东西成为公共类,然后将swing工作者创建为非静态内部类。这样,“done”方法可以轻松访问所有GUI私有变量。

答案 1 :(得分:0)

更好的解决方案是为您的SwingWorker提供GUI实现的接口实例。这样你就可以让你的工作者更新GUI。

public class GUI implements GUI_INTERFACE{
....
new foo(this);

@Override
public void INTERFACE_METHOD(Object info){
//Update Gui variables here.
}

}

对于您的SwingWorker:

class foo extends SwingWorker{
GUI_INTERFACE int
public foo(GUI_INTERFACE bar){
int=bar
}


}