When exactly is the Event Dispatch Thread started?

后端 未结 3 1981
臣服心动
臣服心动 2020-12-17 15:56

When exactly is the EDT started? What line of code is responsible of it?

My guess is that \"someSwingComponent.setVisible(true)\" does the trick, but I\'m not sure.<

3条回答
  •  轮回少年
    2020-12-17 16:48

    • you are right setVisible for any AWT / Swing container starting EDT, non safe way

    • Initial Thread is about the safest way

    • if all events are done in the present EDT, then isDispatchThread returns false

    • in the case that isDispatchThread returns false, then any Swing thread safe methods could alive the EDT, most safest way is invoke that from invokeLater()

    code for test

    import java.awt.Color;
    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.getContentPane().setBackground(Color.red);
                                    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;
        }
    }
    

提交回复
热议问题