-
December 18th, 2012, 01:03 AM
#1
Does SwingUtilities.invokeLater() keep order with threads?
Hi there,
I think I have a threading issue but it all depends on how invokeLater() works. From my understanding invokeLater() will place each thread on a queue and be handled. The question is does it handle it in order? For example, if I put thread1 on the queue first, does it gaturnetee thread1 will be served up first, or could thread2 be called before thread1?
Code:
public static void main(String args[]) {
System.out.println("BEFORE RUN");
SwingUtilities.invokeLater(
new Runnable()
{
@Override
public void run() {
try {
System.out.println("SLEEPING...");
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("TEST1");
}
});
SwingUtilities.invokeLater(
new Runnable()
{
@Override
public void run() {
System.out.println("TEST2");
}
});
}
I keeping getting the same output:
BEFORE RUN
SLEEPING...
TEST1
TEST2
Thanks
-
December 18th, 2012, 12:17 PM
#2
Re: Does SwingUtilities.invokeLater() keep order with threads?
invokeLater() places the Runnable object (not the thread) on the Event Dispatch Queue. The Event Dispatch Thread (EDT) takes Runnable objects off this queue in FIFO order and runs them.
The output is as it is because both runnable objects are run by the same thread (EDT) and so is doesn't matter how long a delay you put in the first object, the second won't run until the first has completed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|