SwingWorker und Event Dispatch Thread (EDT)

  • Beitrags-Autor:
  • Beitrags-Kategorie:Java
System.out.println("TID=" + 
      Thread.currentThread().getId() + " (main)");
final SwingWorker<String, String> x = 
      new SwingWorker<String, String>() {
   @Override
   protected String doInBackground() throws Exception {
      final long tid = Thread.currentThread().getId();
      System.out.println("TID=" + tid + 
            " doInBackground() isEventDispatchThread=" +
            SwingUtilities.isEventDispatchThread());
      System.out.println("Long running code goes here.");
      return "";
   }
   @Override
   protected void done() {
      final long tid = Thread.currentThread().getId();
      System.out.println("TID=" + tid + 
            "          done() isEventDispatchThread=" +
            SwingUtilities.isEventDispatchThread());
      System.out.println("GUI updates/changes go here.");
   }
};
x.execute();
TID=1 (main)
TID=9 doInBackground() isEventDispatchThread=false
Long running code goes here.
TID=16          done() isEventDispatchThread=true
GUI updates/changes go here.