1 Replies - 503 Views - Last Post: 03 October 2014 - 07:07 AM Rate Topic: -----

#1 evert67   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 24-June 14

Why should Swing GUI code be placed on the EDT?

Posted 03 October 2014 - 04:13 AM

According to what I read, “when programming in Swing, your GUI creation code should be placed on the Event Dispatch Thread (EDT). This will prevent potential race conditions that could lead to deadlock.” (See below for code.)

Why is this? How could making a GUI lead to deadlock?

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Why should Swing GUI code be placed on the EDT?

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Why should Swing GUI code be placed on the EDT?

Posted 03 October 2014 - 07:07 AM

Imagine a scenario where one panel is already created and its children are repainting themselves at a set time on the event thread with the panel's paint object. Now imagine someone creates and shows another component on a different thread. Both threads know nothing about one another. The jvm can only do one thing at a time. What happens when the panel on the event thread starts painting at the exact time the panel on the non-event thread starts painting. It is impossible for both to paint at the exact same time, so what happens? Could this ever even happen?

This post has been edited by farrell2k: 03 October 2014 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1