Taming Swing Threads Part 1: The Event Dispatch Thread

Why do I need to know what the Event Dispatch Thread (EDT).

As far as a topic goes for my first blog, discussing the Event Dispatch Thread is probably not the most exciting topic for some, but it is definitely one of the most important as far as swing is concerned.


If you have ever wondered (or shouted while pulling your hair out in frustration)

  • Why does my application go gray or become unresponsive while I’m processing?
  • Why is my swing application slow?
  • Or why do I seem to get random paint errors from swing?

The answer is the EDT and the painful truth is that you are probably not using it optimally.


However don’t get intimidated by the name, the Event Dispatch Thread (EDT) sounds a bit complex, but understanding it is in fact really easy. Once you have this piece of knowledge tucked safely under your belt, you will truly be able to make swing fly, and built the killer applications you have always wanted.

Swing is a single threaded model.

Before we delve into the EDT, I want to discuss how swing is designed and what the implications are, for us as swing developers.


Swing has been designed around a single threaded model. What this means is that swing has been designed to perform all its operations (from painting, capturing mouse clicks etc…) from a single thread. As far as the designers of swing are concerned, this makes their lives far easier as they do not have to deal with concurrency. It is assumed that all swing methods calls and object interactions will all take place on the same single thread.


However, if other threads (processes) attempts to manipulate a swing component, this is considered a violation of swing’s threading model and swing can not guarantee that strange errors will not occur (and trust me, I have some scary stories about this sort of thing).


But how does this relate to the EDT?

The name of the single thread where swing runs, is called the Event Dispatch Thread. The reason for the name is that all our interactions with a swing front end create events. Moving our mouse, typing text, clicking on buttons, all create events that need to be processed. The EDT is responsible for processing all of our interactions (in the form of events). The EDT is the master swing thread, and no one other than itself is allowed to interact with swing components.


Whenever a swing event takes place, that event is placed onto a simple sequential queue called the Event Queue. The events are processed sequentially in the order they were received, (for those of you traumatized by first year accounting, think FIFO, for those who don’t know what FIFO, just be grateful!).


The Event Queue.

I find the best way to illustrate the Event Queue is with an example and a diagram.

Assume a user enters some text into a textbox and then clicks a button. The sequential nature of the queue means that the text capture event will be placed on the queue (at position 1) before the button click event (at position 2). These events will be processed in the order they were added to the queue.



Key Learnings.

The key piece of information that I want you to take away from this blog, is that:

  • Only the EDT may safely manipulate swing components (it is the uber master swing thread).
  • If any other threads are manipulating swing components, they are doing so in an unsafe manner (and bad things will happen).
  • The EDT processes events using a simple sequential queue called the Event Queue.




Reply to comment | Custom Swing Components for the Java™

What's up, everything is going fine here and ofcourse every one is sharing facts, that's truly excellent, keep up writing.

The LATEST chatter

  • Halted all development on the twitter client. The application in its incomplete state is available at: http://tinyurl.com/6ktd8xw 8 years 17 weeks ago
  • Java Swing Components is currently undergoing a rebranding exercise to Custom Swing Components. The url however will remain the same. 8 years 40 weeks ago
  • Java Swing Components is proud to announce the release of our rater component. http://www.javaswingcomponents.com/product/rater 8 years 41 weeks ago
  • Java Swing Components is proud to announce the release of our first bundle including a fun demo. http://www.javaswingcomponents.com/products 8 years 45 weeks ago
  • New post: New Component Teaser http://tinyurl.com/35hxfnn 8 years 46 weeks ago

User login


Syndicate content