Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing - running on EDT

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. Which operations are required to run on the EDT? If they don't, is simply an Exception raised?
  2. Are there any specific times when we actually are on the EDT automatically?
  3. If we schedule a task using SwingUtilities.invokeLater we enqueue it to the current queue of GUI update tasks (the EDT) right?
  4. Accesses to the above queue I guess are synchronized, or some concurrent collection is used, but if I schedule two GUI update tasks, from two background threads, it is impossible to say which one will be added first? For instance, if Thread 1 FIRST submits a task of setting the text of a JLable to either "yes", and then, short time later, second thread comes along and submits task of setting that value to "no", are we guaranteed that the result will be "yes", or is it simply a matter of how these things are scheduled by the OS?
  5. How exactly does the SwingWorker ensure that done() method is run on EDT? It sets the following code:

      future = new FutureTask<T>(callable) {
                   @Override
                   protected void done() {
                       doneEDT();
                       setState(StateValue.DONE);
                   }
               };
    

so I was wondering whether FutureTask somehow makes sure that invokeLater is called?

Thanks for all your answers.

like image 515
Bober02 Avatar asked Aug 06 '12 09:08

Bober02


People also ask

What is EDT in Swing in Java?

The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue.

Is Java Swing thread safe?

No, Java Swing components are not thread-safe in Java.

Is Java Swing single threaded?

Swing is single-threaded.

Is swing part of JDK?

Originally distributed as a separately downloadable library, Swing has been included as part of the Java Standard Edition since release 1.2. The Swing classes and components are contained in the javax. swing package hierarchy.


1 Answers

  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);
         }
     }
    
like image 126
Robin Avatar answered Sep 21 '22 23:09

Robin