The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » Swing & AWT

Thread: What is the threading model of Swing?

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 12 - Last Post: Jun 7, 2006 9:36 AM by: ylzhao
ylzhao

Posts: 33
What is the threading model of Swing?
Posted: May 29, 2006 2:23 PM
  Click to reply to this thread Reply

Hi,
I have noted that threading model is very important to Swing developers in several places, like in http://java.sun.com/javaone/sf/java_rock_star/romain_guy.jsp.
Where can I learn more for the threading model of Swing?
Thanks!

leouser

Posts: 1,160
Re: What is the threading model of Swing?
Posted: May 29, 2006 2:45 PM   in response to: ylzhao
  Click to reply to this thread Reply

I think of Swing as a single-threaded library. Doing stuff outside of the EDT should be considered "illegal"(not sure if you can go to jail for doing it, but it can certainly hang your application). With that said, there are some places where you can do multi-threaded swing stuff, but only do that if the javadoc clearly states "multi-thread safe". If I remember right, AbstractDocument.setCharacterAttributes() may be such a method.

A couple of useful utility methods if you are doing Swing things in another Thread are:
SwingUtilities.invokeLater
SwingUtilities.invokeAndWait

There is also the SwingWorker class, which becomes official in Java 6. I believe it allows you to do work outside of the EDT and it will manage updates for you.

I don't have a good web page reference at the top of my head but I think those are ok tips to keep in your mind when dealing with Swing.

leouser

coxcu

Posts: 184
Re: What is the threading model of Swing?
Posted: May 30, 2006 6:24 AM   in response to: ylzhao
  Click to reply to this thread Reply

The best place to start is Ben Galbraith's presentation on Java Lobby.
http://www.javalobby.org/members-only/eps/galbraith-swing-2/index.html?source=archives

There have been some really good presentations at JavaOne over the years, but I'm not sure how many are still available.

Here's a discussion that you might be interested in.
http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html

Scott Violet
Re: What is the threading model of Swing?
Posted: May 30, 2006 7:51 AM   in response to: ylzhao
  Click to reply to this thread Reply

[nobr]Here's some boilerplate that may make it's way into the official doc
shortly:

/**
* In general Swing is not thread safe. All Swing components and related
* classes, unless otherwise documented, must be accessed on the event
* dispatch thread.
*


* The preferred way to start a Swing application is
* wrap all construction in an invokeLater. Either of the following will
* work equally well:
* <pre>
* public class Main implements Runnable {
* public void run() {
* // Invoked on the event dispatch thread.
* // Construct and show GUI.
* }
* public static void main(String[] args) {
* SwingUtilities.invokeLater(new Main());
* }
* }
* </pre>
* Or:
* <pre>
* public class Main {
* Main() {
* // Invoked on the event dispatch thread.
* // Construct and show GUI.
* }
* public static void main(String[] args) {
* //Schedule a job for the event-dispatching thread:
* //creating and showing this application's GUI.
* SwingUtilities.invokeLater(new Runnable() {
* public void run() {
* new Main();
* }
* });
* }
* }
* </pre>
* This restriction applies to any models attached to Swing components.
* For example, if you have a TableModel attached to a JTable you should
* only modify the TableModel on the event dispatch thread. If you modify
* the model on a separate thread you run the risk of random exceptions and
* possible display corruption.
*


* More information on this topic can be found in the
* Swing
tutorial
,
* in particular the section on
* How
to Use Threads
.
*/

-Scott

swing@javadesktop.org wrote:
> Hi,
> I have noted that threading model is very important to Swing developers in several places, like in http://java.sun.com/javaone/sf/java_rock_star/romain_guy.jsp.
> Where can I learn more for the threading model of Swing?
> Thanks!
> [Message sent by forum member 'ylzhao' (ylzhao)]
>
> http://forums.java.net/jive/thread.jspa?messageID=117190
[/nobr]

coxcu

Posts: 184
Re: What is the threading model of Swing?
Posted: May 30, 2006 8:12 AM   in response to: Scott Violet
  Click to reply to this thread Reply

Scott,

I've begun using factory methods to ensure that construction of any JComponent that I extend only happens on the EDT. This seems like an obvious precaution, yet I haven't seen it much else where.

Is there any chance of Swing adopting the Builder pattern?
https://www28.cplan.com/javaone06_cv_124_1/sessions_catalog.jsp?ilc=124-1&ilg=english&isort=1&is=%3CISEARCH%3E&ip=no&itrack=+&isession_type=+&isession_id=TS-1512&iabstract=&ispeaker=&ispk_company=
Thanks,
Curt

c_armstrong

Posts: 3
Re: What is the threading model of Swing?
Posted: May 30, 2006 9:12 AM   in response to: coxcu
  Click to reply to this thread Reply

Curt,

Do you builders do anything after the components have been realized? As long as you 'realize' the component at the end of the construction processs, constructing them is thread safe. Apologies if I misunderstood your point.

Charles.

tackline

Posts: 238
Re: What is the threading model of Swing?
Posted: May 30, 2006 9:43 AM   in response to: c_armstrong
  Click to reply to this thread Reply

Swing is not only not thread-safe, it is actually thread-hostile. Notable example are JFrame.pack and pretty much anything to do with documents connected to text components. So, construct your components on the EDT.

c_armstrong

Posts: 3
Re: What is the threading model of Swing?
Posted: May 30, 2006 12:08 PM   in response to: tackline
  Click to reply to this thread Reply

tackline,

as noted in the link Hans has just linked to about the thread rule, pack() counts as realization. Following the guidelines in the thread rule does work.

hth,
Charles.

ylzhao

Posts: 33
Re: What is the threading model of Swing?
Posted: May 30, 2006 8:27 AM   in response to: ylzhao
  Click to reply to this thread Reply

Hi,
I install JDK 6.0 b85 and NetBeans 5.5 beta, and I want to test the SplashScreen class of JDK 6.0.
However, I am a bit curious about this scenario:
-----------------SplashDemo.java-------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SplashDemo {

/** Creates a new instance of SplashDemo */
public SplashDemo() {
}

public static void main(String[] args) {
Runnable runner = new Runnable() {
public void run() {
SplashScreen splash = SplashScreen.getSplashScreen();
Graphics2D g = splash.createGraphics();
Dimension dim = splash.getSize();
int x = 0;
int y = 225;
int length = 100;
int width = 5;
for (int i = 0; i < length; i++) {
g.setColor(Color.RED);
g.drawString("Loading...", 150, 210);
g.setColor(Color.MAGENTA);
g.fillRect(x, y, x + i * dim.width /

Hans Muller
Re: What is the threading model of Swing?
Posted: May 30, 2006 10:09 AM   in response to: ylzhao
  Click to reply to this thread Reply


Kathy Walrath and I wrote about the Swing threading model quite some
time ago:

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

An improved version of the SwingWorker class that's covered in these
articles has become part of Mustang, and is available for Tiger (1.5):
https://swingworker.dev.java.net/

- Hans


swing@javadesktop.org wrote:
> Hi,
> I have noted that threading model is very important to Swing developers in several places, like in http://java.sun.com/javaone/sf/java_rock_star/romain_guy.jsp.
> Where can I learn more for the threading model of Swing?
> Thanks!
> [Message sent by forum member 'ylzhao' (ylzhao)]
>
> http://forums.java.net/jive/thread.jspa?messageID=117190
>


ylzhao

Posts: 33
Re: What is the threading model of Swing?
Posted: Jun 4, 2006 4:39 PM   in response to: Hans Muller
  Click to reply to this thread Reply

Hi,
I read the articles about Swing threading model, and these articles note that the Event Dispatch Thread (EDT) is also an important part.
In my understanding, for every program runs on JVM, it has a Event List and a Event Dispatch Thread, and all the events are pulled into the Event List, then The Event Dispatch Thread starts to dispatch the events in order to the appropriate Event Handler. Am I right?
Also, are there more materials on Event Dispatch Thread?
Thanks!

Scott Violet
Re: What is the threading model of Swing?
Posted: Jun 5, 2006 8:10 AM   in response to: ylzhao
  Click to reply to this thread Reply

swing@javadesktop.org wrote:
> Hi,
> I read the articles about Swing threading model, and these articles note that the Event Dispatch Thread (EDT) is also an important part.
> In my understanding, for every program runs on JVM, it has a Event List and a Event Dispatch Thread, and all the events are pulled into the Event List, then The Event Dispatch Thread starts to dispatch the events in order to the appropriate Event Handler. Am I right?
> Also, are there more materials on Event Dispatch Thread?

Hello,

For a VM that only runs one app, you are correct. We refer to it the
list as an event queue (EventQueue.java). If the VM is running applets,
then you may have multiple event threads and event queues.

-Scott


ylzhao

Posts: 33
Re: What is the threading model of Swing?
Posted: Jun 7, 2006 9:36 AM   in response to: Scott Violet
  Click to reply to this thread Reply

I also have some questions.

For example, if I have two Java applications named MyApp1 and MyApp2, then I run these two apps using "java MyApp1" and "java MyApp1" commands.

Q1: does two JVMs starts to run these two apps?

Q2: does every app have a event queue?

Q3: I note that the event dispatch thread's name is "awt-eventqueue-0". Who start this thread? The JVM?

Q4: How the event dispatch thread dispatch events? My understanding is:
while ((event = getEvent()) != Quit_Event) {
dispatch(event);
}
if there is no event, then the event dispatch thread is blocked at the getEvent() method.

Q5: When an event occcurs, who post this event into the app's event queue, and how?




 XML java.net RSS