title: How does the event dispatch thread work?
body: <p>With the help of people on <a href="https://stackoverflow.com/questions/2479295/how-the-simples-gui-countdown-is-supposed-to-work">stackoverflow</a> I was able to get the following working code of a simple GUI countdown (which just displays a window counting down seconds). My main problem with this code is the <code>invokeLater</code> stuff.</p>
  
  <p>As far as I understand <code>invokeLater</code>, it sends a task to the event dispatching thread (EDT) and then the EDT executes this task whenever it "can" (whatever that means). <strong>Is that right?</strong></p>
  
  <p>To my understanding, the code works like this:</p>
  
  <ol>
  <li><p>In the <code>main</code> method we use <code>invokeLater</code> to show the window (<code>showGUI</code> method). In other words, the code displaying the window will be executed in the EDT.</p></li>
  <li><p>In the <code>main</code> method we also start the <code>counter</code> and the counter (by construction) is executed in another thread (so it is not in the event dispatching thread). Right?</p></li>
  <li><p>The <code>counter</code> is executed in a separate thread and periodically it calls <code>updateGUI</code>. <code>updateGUI</code> is supposed to update the GUI. And the GUI is working in the EDT. So, <code>updateGUI</code> should also be executed in the EDT. It is the reason why the code for the <code>updateGUI</code> is enclosed in <code>invokeLater</code>. Is that right?</p></li>
  </ol>
  
  <p>What is not clear to me is why we call the <code>counter</code> from the EDT. Anyway, it is not executed in the EDT. It starts immediately, a new thread and the <code>counter</code> is executed there. So, <strong>why can we not call the <code>counter</code> in the main method after the <code>invokeLater</code> block?</strong></p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.SwingUtilities;
  
  public class CountdownNew {
  
      static JLabel label;
  
      // Method which defines the appearance of the window.   
      public static void showGUI() {
          JFrame frame = new JFrame("Simple Countdown");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          label = new JLabel("Some Text");
          frame.add(label);
          frame.pack();
          frame.setVisible(true);
      }
  
      // Define a new thread in which the countdown is counting down.
      public static Thread counter = new Thread() {
          public void run() {
              for (int i=10; i&gt;0; i=i-1) {
                  updateGUI(i,label);
                  try {Thread.sleep(1000);} catch(InterruptedException e) {};
              }
          }
      };
  
      // A method which updates GUI (sets a new value of JLabel).
      private static void updateGUI(final int i, final JLabel label) {
          SwingUtilities.invokeLater( 
              new Runnable() {
                  public void run() {
                      label.setText("You have " + i + " seconds.");
                  }
              }
          );
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  showGUI();
                  counter.start();
              }
          });
      }
  
  }
  </code></pre>