Event Dispatching Thread and invokeLater 's argument

Hello there:

I have a question about the Event Dispatching Thread and SwingUtilities.invokeLater.

Normally, we construct a runnable object to include GUI-update code, and pass this object as an argument to SwingUtilities.invokeLater. By doing so the EDT will take over to take care of the component update (for the thread-safe APIs we don't have to do like this).

Question is, I thought EDT is a single thread, but obviously when we create runnable objects as above, a couple new threads have been generated. So how does invokeLater work on EDT to resolve this mystery?

I hope I've mede myself understood,

Thanks for your answers,

Sway

[683 byte] By [fathomBoata] at [2007-11-27 5:14:49]
# 1
EDT is a single thread. Runnable is just an interface with a method. There is nothing special about it that requires another Thread to call it.
bsampieria at 2007-7-12 10:36:52 > top of Java-index,Desktop,Core GUI APIs...
# 2

bsampieri:

Thx for the reply. However, I need to clarify: Runnable is an interface, I know that. When I say a runnable object, what I meant was to construct an object to implement runnable interface, which creates a new thread.

Very often we do like this:

SwingUtilities.invokeLater(new Runnable(){

public void run(){

...

}

});

Supposed if we write code like above a couple times in an app, several threads will be created. My question was, if this is what's happened, how does EDT, which is a single thread, handle these threads? Does it queue them? Or in another word, how does invokeLater work on EDT?

Thanks,

Sway

fathomBoata at 2007-7-12 10:36:52 > top of Java-index,Desktop,Core GUI APIs...
# 3

Creating a Runnable does not start a new Thread. Running a Runnable does not start a new Thread either. A Runnable is just a normal object, like any other object, and its run() method is just a normal method, like any other method.

Now, you can start a new thread in which you run your Runnable, but the Runnable itself will not do that:

Runnable job = new Runnable() {

public void run() {

...

}

};

job.run() // the call to run() is made from the current thread. No new thread is started.

new Thread(job).start(); // you start a new thread, that runs the job

Torgila at 2007-7-12 10:36:52 > top of Java-index,Desktop,Core GUI APIs...
# 4

>

> Supposed if we write code like above a couple times

> in an app, several threads will be created. My

> question was, if this is what's happened, how does

> EDT, which is a single thread, handle these threads?

> Does it queue them? Or in another word, how does

> invokeLater work on EDT?

>

Yes, everytime you call invokeLater(), the Runnable will be placed on the event queue. The EDT will run them one by one, in the same order as they were added.

Torgila at 2007-7-12 10:36:52 > top of Java-index,Desktop,Core GUI APIs...
# 5

SwingUtilities.invokeLater(new Runnable(){

public void run(){

...

}

});

...does not create nor start a new thread.

Only...

new Thread(new Runnable(){

public void run(){

...

}

}).start();

...does create and start a new thread. SwingUtilities.invokeXxx() methods do not do this. It simply dumps the Runnable object into a queue and the EDT will get to it eventually and see it's a Runnable and will call the run() method.

bsampieria at 2007-7-12 10:36:52 > top of Java-index,Desktop,Core GUI APIs...
# 6
Like he said.
bsampieria at 2007-7-12 10:36:52 > top of Java-index,Desktop,Core GUI APIs...
# 7
That's what I realized later, thanks a lot to both of you folks!Sway
fathomBoata at 2007-7-12 10:36:52 > top of Java-index,Desktop,Core GUI APIs...