title: Main Thread vs. UI Thread in Java
body: <p>In many Swing snippets given here as answers, there is a call to <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29"><code>SwingUtilities#invokeLater</code></a> from the <code>main</code> method:</p>
  
  <pre><code>public class MyOneClassUiApp {
  
      private constructUi() {
          // Some Ui related Code
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  new MyOneClassUiApp().constructUi();
              }
          });
      }
  }
  </code></pre>
  
  <p>But according to the <a href="http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html">Threads and Swing article</a>, it is safe to construct UI from main thread:</p>
  
  <blockquote>
    <p>A few methods are thread-safe: In the Swing API documentation,
    thread-safe methods are marked with this text:</p>
    
    <p>This method is thread safe, although most Swing methods are not.</p>
    
    <p>An application's GUI can often be constructed and shown in the main
    thread: The following typical code is safe, as long as no components
    (Swing or otherwise) have been realized:</p>
  </blockquote>
  
  <pre><code>public class MyApplication {
  public static void main(String[] args) {
     JFrame f = new JFrame("Labels");
     // Add components to 
     // the frame here... 
     f.pack(); 
     f.show(); 
     // Don't do any more GUI work here... 
     } 
  }
  </code></pre>
  
  <p>So, is there a real (thread safety) reason to construct the UI in main through <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29"><code>SwingUtilities#invokeLater</code></a>, or this is just a habit, to remember do it in other cases?</p>