|
Replies:
12
-
Last Post:
Jun 7, 2006 9:36 AM
by: ylzhao
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
May 29, 2006 2:45 PM
in response to: ylzhao
|
|
|
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
|
|
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
May 30, 2006 7:51 AM
in response to: ylzhao
|
|
|
[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]
|
|
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
May 30, 2006 9:12 AM
in response to: coxcu
|
|
|
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.
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
May 30, 2006 9:43 AM
in response to: c_armstrong
|
|
|
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.
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
May 30, 2006 12:08 PM
in response to: tackline
|
|
|
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.
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
May 30, 2006 8:27 AM
in response to: ylzhao
|
|
|
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 /
|
|
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
Jun 4, 2006 4:39 PM
in response to: Hans Muller
|
|
|
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!
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
Jun 5, 2006 8:10 AM
in response to: ylzhao
|
|
|
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
|
|
|
|
|
|
|
|
Re: What is the threading model of Swing?
Posted:
Jun 7, 2006 9:36 AM
in response to: Scott Violet
|
|
|
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?
|
|
|
|
|