• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Event dispatch thread

 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a quick question : If i run something like :


Then would any action event and the swing code would run inside the EventDispatch thread or do we have to use the


then the code would run inside the eventdispatch thread ?
I tried reading some articles but not really clear with threads .
 
Stephan van Hulst
Bartender
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are referring to code you would put in your TestFr constructor. No, this will be run by the main thread, not by the EDT.

However, this is not a problem. As long as you haven't started up the EDT (which you do through the first call to pack() or setVisible() on a JFrame) you can call any methods on your Swing components. You don't have to worry about deadlock, because the EDT isn't there to hold resources yet.

After your frame becomes visible (or you call pack()) you have to start calling Swing component methods through the EDT. This mostly happens naturally through event listeners. Usually, there isn't a cause to call the invokeAndWait() or invokeLater() methods.
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Stephen Thanks for the reply . Let's assume that i do call the setVisible() method . Then would the EDT be responsible for any further executions or calculations in the TestFr .

And another(final) question is that let's say i put my code of TestFr in the eventqueue and the code starts running a lengthy calculation and then the user tries to cancel the application . Then will that cancel request be queued in the EventQueue or would be handled seperately ?? Thanks for your time .
 
Stephan van Hulst
Bartender
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
budsy remo wrote:Let's assume that i do call the setVisible() method . Then would the EDT be responsible for any further executions or calculations in the TestFr .

Not all executions in your TestFr have to be called on the EDT. Just those that involve Swing/AWT components (including operations on your frame itself).

And another(final) question is that let's say i put my code of TestFr in the eventqueue and the code starts running a lengthy calculation and then the user tries to cancel the application . Then will that cancel request be queued in the EventQueue or would be handled seperately ?? Thanks for your time .


Depends on what you mean by 'cancel'. If you mean externally shutting down the application through a task manager or through the console, or by calling System.exit(), then the application will terminate immediately. If you attempt to close the application through the system menu or the close button, this will trigger an event that ends up on the EDT, which will of course only be handled after the lengthy operation (and others) have finished.

You should never call long lasting operations on the EDT, because this makes your application unresponsive. They should be handled by SwingWorkers instead.
 
Rob Camick
Ranch Hand
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, this is not a problem. As long as you haven't started up the EDT (which you do through the first call to pack() or setVisible() on a JFrame) you can call any methods on your Swing components. You don't have to worry about deadlock, because the EDT isn't there to hold resources yet.


Sun/Oracle's newer recommendation is that ALL code the create GUI components or interacts with the GUI should be done on the EDT. This includes the creation of the frame and its components even before a pack() method is invoked. Take a look at all the examples is the Swing tutorial.

http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, and tangential to this discussion, many of the Swing methods presently documented as being thread-safe will no longer be so documented in JDK7, It has been accepted that those methods in fact are not thread-safe, never were thread-safe and it may be considered a documentation bug.

I'm not sure, but I think in JDK7 there won't be any Swing methods documented as thread-safe.
 
Stephan van Hulst
Bartender
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, Darryl
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Darryl ,Stephen and Rob ...thanks a million.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic