2012-04-22 4 views
0

Как заявлено Sun, что actionPerformed() из ActionListener выполняется в EDT автоматически, поэтому я решил создать новый поток внутри метода actionPerformed(), и проблема в этом нить не работает внутри EDT. Может кто-нибудь объяснить это? Спасибосоздать новую тему внутри actionPerformed и Event Dispatch Thread (EDT) issue

ответ

3

Если вы создаете новый поток из EDT, этот поток отличается от EDT. Разве это не ясно?
Вы должны обновлять элементы управления через EDT.
Вы можете создавать собственные потоки фона для тяжелых задач, но обновление элементов управления должно осуществляться только через EDT. Существуют конструкции, которые вы можете использовать для облегчения вашего кода, например. передайте Runnable, который вызывается EDT через SwingUtilities invoke Runnable. Вы должны изучить около Concurrency in Swing

+0

'invokeAndWait' не удается бежать из' actionPerformed() ', так как этот поток может быть закончена в' EDT', а затем возвращает несколько 'exception', а не хорошее предложение – mKorbel

+0

Привет я знаю что контрольные обновления должны быть внутри EDT. Я просто подумал, что недавно созданный поток будет делиться EDT, поскольку он уже находится внутри среды EDT>. < – MinhHoang

+0

@mKorbel: Хорошая точка. Но я подозреваю, что он хотел создать фоновый поток для какой-то задачи, а затем обновить графический интерфейс, который является обычным error of incomers to Swing.I будет обновлять ответ, чтобы уточнить это. – Cratylus

1

1) AWT или Качели GUI испробованы и создали EDT, это событие сделано (может быть, есть другой) методы пакета() и SetVisible (истина)

2) если все события, ожидающие в EDT и EDT пуст, то if (EventQueue.isDispatchThread()) {возвращает false, но поток EDT существует до тех пор, пока не будет существовать текущий экземпляр JVM

3) как указано для вызова очереди EDT, вы можете использовать invokeLater или invokeAndWait, уведомление invokeAndWait вы можете использовать, только если isDispatchThread возвращает false, в противном случае возвращает исключения

4), вызвав Thread.sleep (int) из Swing Listener или его метод может заморозить и заблокировать EDT, что приведет к потере какого-либо события из очереди событий,

Пример двух кодов для тестирования isDispatchThread() и как живой EDT из Java Предметы

import java.awt.EventQueue; 
import java.lang.reflect.InvocationTargetException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.concurrent.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class IsThereEDT { 

    private ScheduledExecutorService scheduler; 
    private AccurateScheduledRunnable periodic; 
    private ScheduledFuture<?> periodicMonitor; 
    private int taskPeriod = 30; 
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
    private Date dateRun; 
    private JFrame frame1 = new JFrame("Frame 1"); 

    public IsThereEDT() { 
     scheduler = Executors.newSingleThreadScheduledExecutor(); 
     periodic = new AccurateScheduledRunnable() { 

      private final int ALLOWED_TARDINESS = 200; 
      private int countRun = 0; 
      private int countCalled = 0; 
      private int maxCalled = 10; 

      @Override 
      public void run() { 
       countCalled++; 
       if (countCalled < maxCalled) { 
        if (countCalled % 3 == 0) { 
         /*if (EventQueue.isDispatchThread()) { 
          SwingUtilities.invokeLater(new Runnable() { 

           @Override 
           public void run() { 
            //some stuff 
           } 
          }); 
         } else { 
          try { 
           SwingUtilities.invokeAndWait(new Runnable() { 

            @Override 
            public void run() { 
             //some stuff 
            } 
           }); 
          } catch (InterruptedException ex) { 
           Logger.getLogger(IsThereEDT.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(IsThereEDT.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         }*/ 
         SwingUtilities.invokeLater(new Runnable() { 

          @Override 
          public void run() { 
           System.out.println("Push a new event to EDT"); 
           frame1.repaint(); 
           isThereReallyEDT(); 
          } 
         }); 
        } else { 
         if (this.getExecutionTime() < ALLOWED_TARDINESS) { 
          countRun++; 
          isThereReallyEDT(); // non on EDT 
         } 
        } 
       } else { 
        System.out.println("Terminating this madness"); 
        System.exit(0); 
       } 
      } 
     }; 
     periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.SECONDS); 
     periodic.setThreadMonitor(periodicMonitor); 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       isThereReallyEDT(); 
       frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame1.getContentPane().add(new JLabel("Hello in frame 1")); 
       frame1.pack(); 
       frame1.setLocation(100, 100); 
       frame1.setVisible(true); 
      } 
     }); 
     try { 
      Thread.sleep(500); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(IsThereEDT.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame2 = new JFrame("Frame 2"); 
       frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame2.getContentPane().add(new JLabel("Hello in frame 2")); 
       frame2.pack(); 
       frame2.setLocation(200, 200); 
       frame2.setVisible(true); 
       isThereReallyEDT(); 
      } 
     }); 
    } 

    private void isThereReallyEDT() { 
     dateRun = new java.util.Date(); 
     System.out.println("       Time at : " + sdf.format(dateRun)); 
     if (EventQueue.isDispatchThread()) { 
      System.out.println("EventQueue.isDispatchThread"); 
     } else { 
      System.out.println("There isn't Live EventQueue.isDispatchThread, why any reason for that "); 
     } 
     if (SwingUtilities.isEventDispatchThread()) { 
      System.out.println("SwingUtilities.isEventDispatchThread"); 
     } else { 
      System.out.println("There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that "); 
     } 
     System.out.println(); 
    } 

    public static void main(String[] args) { 
     IsThereEDT isdt = new IsThereEDT(); 
    } 
} 

abstract class AccurateScheduledRunnable implements Runnable { 

    private ScheduledFuture<?> thisThreadsMonitor; 

    public void setThreadMonitor(ScheduledFuture<?> monitor) { 
     this.thisThreadsMonitor = monitor; 
    } 

    protected long getExecutionTime() { 
     long delay = -1 * thisThreadsMonitor.getDelay(TimeUnit.MILLISECONDS); 
     return delay; 
    } 
} 

и простой код

.

.

import java.awt.EventQueue; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.concurrent.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class IsThereEDT { 

    private ScheduledExecutorService scheduler; 
    private AccurateScheduledRunnable periodic; 
    private ScheduledFuture<?> periodicMonitor; 
    private int taskPeriod = 30; 
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
    private Date dateRun; 
    private JFrame frame1 = new JFrame("Frame 1"); 

    public IsThereEDT() { 
     scheduler = Executors.newSingleThreadScheduledExecutor(); 
     periodic = new AccurateScheduledRunnable() { 

      private final int ALLOWED_TARDINESS = 200; 
      private int countRun = 0; 
      private int countCalled = 0; 
      private int maxCalled = 10; 

      @Override 
      public void run() { 
       countCalled++; 
       if (countCalled < maxCalled) { 
        if (countCalled % 3 == 0) { 
         SwingUtilities.invokeLater(new Runnable() { 

          @Override 
          public void run() { 
           System.out.println("Push a new event to EDT"); 
           frame1.repaint(); 
           isThereReallyEDT(); 
          } 
         }); 
        } else { 
         if (this.getExecutionTime() < ALLOWED_TARDINESS) { 
          countRun++; 
          isThereReallyEDT(); // non on EDT 
         } 
        } 
       } else { 
        System.out.println("Terminating this madness"); 
        System.exit(0); 
       } 
      } 
     }; 
     periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.SECONDS); 
     periodic.setThreadMonitor(periodicMonitor); 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       isThereReallyEDT(); 
       frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame1.getContentPane().add(new JLabel("Hello in frame 1")); 
       frame1.pack(); 
       frame1.setLocation(100, 100); 
       frame1.setVisible(true); 
      } 
     }); 
     try { 
      Thread.sleep(500); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(IsThereEDT.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame2 = new JFrame("Frame 2"); 
       frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame2.getContentPane().add(new JLabel("Hello in frame 2")); 
       frame2.pack(); 
       frame2.setLocation(200, 200); 
       frame2.setVisible(true); 
       isThereReallyEDT(); 
      } 
     }); 
    } 

    private void isThereReallyEDT() { 
     dateRun = new java.util.Date(); 
     System.out.println("       Time at : " + sdf.format(dateRun)); 
     if (EventQueue.isDispatchThread()) { 
      System.out.println("EventQueue.isDispatchThread"); 
     } else { 
      System.out.println("There isn't Live EventQueue.isDispatchThread, why any reason for that "); 
     } 
     if (SwingUtilities.isEventDispatchThread()) { 
      System.out.println("SwingUtilities.isEventDispatchThread"); 
     } else { 
      System.out.println("There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that "); 
     } 
     System.out.println(); 
    } 

    public static void main(String[] args) { 
     IsThereEDT isdt = new IsThereEDT(); 
    } 
} 

abstract class AccurateScheduledRunnable implements Runnable { 

    private ScheduledFuture<?> thisThreadsMonitor; 

    public void setThreadMonitor(ScheduledFuture<?> monitor) { 
     this.thisThreadsMonitor = monitor; 
    } 

    protected long getExecutionTime() { 
     long delay = -1 * thisThreadsMonitor.getDelay(TimeUnit.MILLISECONDS); 
     return delay; 
    } 
} 
Смежные вопросы