• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Event Dispatch Thread

 
Ranch Hand
Posts: 47
PHP C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Today I read this article : Threads and Swings. I got few questions, please help me finding answers.

1 . An applet's GUI can be constructed and shown in the init() method: Existing browsers don't draw an applet until after its init() and start() methods have been called. Thus, constructing the GUI in the applet's init() method is safe, as long as you never call show() or setVisible(true) on the actual applet object.

What is happening in the stroked line ?

2. Please explain this : The initialization should not occur in the event-dispatching thread; otherwise, repainting and event dispatch would stop.

3. invokeLater(): Requests that some code be executed in the event-dispatching thread. This method returns immediately, without waiting for the code to execute.

What do they mean by return immediately ? What's the use of the code if it is not executed ?

4. Runnable doWorkRunnable = new Runnable() {
public void run() { doWork(); }
};
Is that any name given to these kind of interface implementation, like anonymous interfaceimplementation ?

Thanks a lot, you guys are awesome.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are two levels at which you might try to understand this. The lesser level is to understand what is meant by Swing's rules, and follow them. You could go for a 2nd level in which you try to understand what Swing is doing "under the covers". I am going to attempt an explanation on the first level, there are only parts of the 2nd level I understand anyway.

Sagar Dabas wrote:Today I read this article : Threads and Swings. I got few questions, please help me finding answers.

1 . An applet's GUI can be constructed and shown in the init() method: Existing browsers don't draw an applet until after its init() and start() methods have been called. Thus, constructing the GUI in the applet's init() method is safe, as long as you never call show() or setVisible(true) on the actual applet object.

What is happening in the stroked line ?


A rule in Swing is that you never call methods to do anything with Swing things except on the event dispatch thread. You can do this by knowing that the routine you are in is only called on that thread (for instance, an action routine called when a button is clicked) or by forcing that code into the thread with Swing's invokeLater or invokeAndWait functions.
[/quote=Sagar Dabas]

The rule applies any time the Swing objects have been "realized", which means that they've had their memory structures filled up, laid out, whatever the heck Swing does when it's going to put things on the screen. And although realization of things is associated with drawing them on the screen, there are cases where they are realized but still aren't on the screen, so you can't equate them exactly.

In the line you underlined, they are saying that this is an exception to that rule. You ARE allowed to do things with Swing in init() and start() on applets, as long as you don't do things to get them realized (like show() and setVisible(), and in fact pack()).

2. Please explain this : The initialization should not occur in the event-dispatching thread; otherwise, repainting and event dispatch would stop.

This is in a section on a "lengthy initialization"; you don't want to do anything that takes a significant amount of elapsed time in the dispatch thread, because that holds up the repainting and other things that take place there. For instance, it is on that thread that your mouse cursor is updated; you don't want that held up while your program accesses a database or reads in a file.

Sagar Dabas wrote:

3. invokeLater(): Requests that some code be executed in the event-dispatching thread. This method returns immediately, without waiting for the code to execute.

What do they mean by return immediately ? What's the use of the code if it is not executed ?


You call invokeLater, your call returns immediately, and the code you handed to invokeLater is called on the dispatch thread. Welcome to multi-threaded programming.

Sagar Dabas wrote:


4. Runnable doWorkRunnable = new Runnable() {
public void run() { doWork(); }
};
Is that any name given to these kind of interface implementation, like anonymous interfaceimplementation ?

Thanks a lot, you guys are awesome.



The doWork() method is just an example; it is your method, call it what you want. No, it isn't part of an interface or anything.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thus, constructing the GUI in the applet's init() method is safe, as long as you never call show() or setVisible(true) on the actual applet object.



That article is over 10 years old. Advice has changed in the meantime. It is now suggested that the GUI creation should be done on the EDT using the invokeAndWait() method. See: How to Make Applets for more information. You should also read the "Concurreny in Swing" section.
 
Sagar Dabas
Ranch Hand
Posts: 47
PHP C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much , now I understood . If you have time , please check these lines .

1. For Applets , GUI creation should be done in init() method because the componenets will be painted or in other words components would be realized only after init() and start() method is invoked, so it is thread safe .

But as Rob Camick told, now GUI creation in not safe even before realization ( I don't know why ? ). All GUI creation and updation should be done in EDT thread.

2. Thanks Ralph Cook, I thought it was for every case , not just only for lengthy initialization.

3. invokeLater() : I got it now, this function passes the Runnable object to another class which wraps it in a AWTEvent subclass , and convert it into a RunnableEvent and finally, as it is a event, it can be enqueued to the event dispatch thread . EDT will call RunnableEvent's run() method whenever EDT have free time.
And in case of invokeAndWait(), the function is not returned until it's run() method is executed by EDT.

4. The last question was stupid, that's a anonymous inner class. I was talking about Runnable interface implentation not doWork() method.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic