title: The SwingUtilities.invokeLater() method in a Java Swing program
body: <p>I am studying Java Swing and I have some questions related to this simple code tutorial that I am reading:</p>
  <pre><code>package com.andrea.execute;
  
  import javax.swing.JFrame;
  import javax.swing.SwingUtilities;
  
  /* An istance of a SimpleExample class is a JFrame object: a top level container */
  public class SimpleExample extends JFrame {
  
      /* Create a new SimpleExample object (that is a JFrame object) and set some property */
      public SimpleExample() {
          setTitle(&quot;Simple example&quot;);
          setSize(300, 200);
          setLocationRelativeTo(null);                // Center the window on the screen. 
          setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
  
      public static void main(String[] args) {
  
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  SimpleExample ex = new SimpleExample();
                  ex.setVisible(true);
              }
          });
      }
  }
  </code></pre>
  <p>The logic is pretty simple: I have a <strong>SimpleExample</strong> class that inherits from <strong>JFrame</strong> Swing class. So a <strong>SimpleExample</strong> will be a toplevel container.</p>
  <p>This class contain also the <strong>main()</strong> method and now I have 2 doubts:</p>
  <p><strong>1) Why in the main() method is execute this code:</strong></p>
  <pre><code>SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  SimpleExample ex = new SimpleExample();
                  ex.setVisible(true);
              }
          });
  </code></pre>
  <p>It call the <strong>invokeLater()</strong> method on the SwingUtilities class and pass to it a new Runnable oject.</p>
  <p>Reading the documentation I know that is a way to places the application on the Swing Event Queue. It is used to ensure that all UI updates are concurrency-safe.</p>
  <p>The thing that I have some problem to understand is how it is implemented.</p>
  <p>Inside the <strong>input parameter</strong> of <strong>invokeLater()</strong> method it pass this &quot;stuff&quot;:</p>
  <pre><code>new Runnable() {
          @Override
          public void run() {
              SimpleExample ex = new SimpleExample();
              ex.setVisible(true);
          }
      });
  </code></pre>
  <p>What is it this stuff? What does it represent? and How does it work?</p>