• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Why should Swing GUI code be placed on the EDT?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Marshal
Posts: 64627
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find a copy of Horstmann and Cornell Core Java2 (vol I) and it says that it has never proven possible to create a thread‑safe GUI framework. They show the example of an input from a different thread which throws array out of bounds exceptions. That is why you should always access GUI components via the same thread, and Java® uses the EDT.
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But if my entire program runs in one thread (i.e. I never create any threads), do I still need the above code to create a GUI?
 
Campbell Ritchie
Marshal
Posts: 64627
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

You may find the JVM moves the GUI execution onto the EDT. I am not sure. Then you are running two threads, and you have the possibility of calling things twice.

Try a Thread.currentThread() call and print its name. That will show whether you are on the EDT. Try starting a GUI from the main method, then call this sort of thing:-
System.out.println(1 / 0);
See whether the GUI vanishes after that Exception.
 
Rancher
Posts: 3051
22
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
99.9% of the time simply creating components and adding them to a GUI will not cause a problem. But when/if it does cause a problem you don't want to waste time trying to find a problem that randomly occurs. So to be safe do all Swing interactions on the EDT.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Try a Thread.currentThread() call and print its name. That will show whether you are on the EDT.


Or use SwingUtilities#isEventDispatchThread().
 
Saloon Keeper
Posts: 3289
145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some very readable stuff, read the Oracle tutorials:

http://docs.oracle.com/javase/tutorial/essential/concurrency/

and

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

Greetz,
Piet
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!