Java Swing - running on EDT

前端 未结 4 1504
一整个雨季
一整个雨季 2020-12-06 17:25

I have a couple of questions with regards to Swing and using EDT for GUI updates. I just started reading on this stuff so I am a full beginner in this area:

  1. Wh
4条回答
  •  半阙折子戏
    2020-12-06 17:50

    SwingWorker ensure that done() method is run on EDT via below code:

        Runnable doDone =
            new Runnable() {
                public void run() {
                    done();
                }
            };
        if (SwingUtilities.isEventDispatchThread()) {
            doDone.run();
        } else {
            doSubmit.add(doDone);
        }
    

    Actually it add the doDone variable into AccumulativeRunnable doSubmit,

    See the source code of AccumulativeRunnable.java you will find there has a below code

    protected void submit() {

    SwingUtilities.invokeLater(this);

    }

    That's why swingworker ensure the method done() running on EDT

提交回复
热议问题