Java Swing - running on EDT

前端 未结 4 1489
一整个雨季
一整个雨季 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:37
    1. A good rule is that all operations (access/updates/...) should happen on the EDT. There are a few exceptions mentioned in the javadoc (certain methods of certain classes), but they are so hard to remember that it is easier to stick to the 'do everything on the EDT' approach. Exceptions will not be raised (luckily, JavaFX fixed this shortcoming). You can use a custom RepaintManager to detect most of these violations: see this article.
    2. Everything triggered by the user is handled on the EDT. For example if the user clicks on a button, the actionPerformed of the corresponding Action or ActionListener will be called on the EDT.
    3. Correct
    4. The thing you schedule first will be executed first. The invokeLater call simply adds the Runnable at the end of the queue. Using invokeLater a second time a bit later will add this new Runnable after the previously scheduled Runnable.
    5. Take a look at the code for doneEDT

      private void doneEDT() {
          Runnable doDone = 
              new Runnable() {
                  public void run() {
                      done();
                  }
              };
          if (SwingUtilities.isEventDispatchThread()) {
              doDone.run();
          } else {
              doSubmit.add(doDone);
          }
      }
      
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-06 17:58
    1. Basically, every time you use a Swing component or a model of a Swing component, it must be done in the EDT. If you don't, no exception will be raised. It could work, but it could also not work, have erratic behavior, corrupted data, etc.
    2. Every Swing event listener is called in the EDT. Basically, except the main method, every line of code of a Swing application is executed in the EDT by default, unless you explicitely start a thread, use a SwingWorker, or something like that.
    3. yes.
    4. Tasks submitted to SwingUtilities.invokeLater() are executed in the same order as the order they were submitted.
    5. Internally, it uses SwingUtilities.invokeLater() or a similar method. The FutureTask doesn't have anything to do with Swing. It's the SwingWorker that ensures that its done method is executed in the EDT. The doneEDT() method has the following comment: Invokes done on the EDT.
    0 讨论(0)
  • 2020-12-06 17:58

    1. In Java GUI applications, main() method is not long lived, after scheduling the construction of GUI in the Event Dispatcher Thread, the main() method quits...and Now its EDT's responsibility to handle the GUI.

    2. So we don't need to start our app on the EDT, its automatically done.

    3 Always keep the UI work on the UI thread, and Non-UI work on the Non-UI thread.

    So Always keep your EDT thread, which is the GUI thread only for GUI work.

    Eg:

    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable(){
              public void run(){    
                myframe.setVisible(true);
             }
         }
    }
    

    4. Create a separate Non-UI thread to handle that long time taking method.

    5. You can simply use a Thread or use SwingWorker which is specially introduced into Java to sync the UI and Non-UI thread.

    6. SwingWorker doesnt ensure that done() method is run on EDT, but sync the output of it to EDT thread, which is the GUI thread.

    0 讨论(0)
提交回复
热议问题