What is the difference between the invokeLater() and invokeAndWait() ?

Before looking at the difference between invokeLater and invokeAndWait , we need to understand that GUI work normally happens in event dispatching thread, most programs driven by events such as button clicks and mouse actions which are handled in event-dispatching thread but sometimes we need some task to do in non event-dispatching thread.
But why we need some programs in non event dispatching thread : There are some situations such as lengthy initializations which can take some time takes also calling other some programs or threads which takes time such as calling method from server.

So why we need invokeLater and invokeAndWait :

Lets consider two conditions discussed below:

1. Suppose we need to show the UI until some code initialization happening but in mean time if we do initialization in event dispatching thread it will create problems in repainting and also in event dispatching, and UI will also get freeze. So If we want to update the UI after initialization done we need to do it in event dispatching thread from non dispatching thread. So here we will need invokeLater or invokeAndWait.

2.Suppose you have a button and on its action you are calling method on the server to get the updates, but you are not sure how much time it will take, in the meantime there will be chances that if we run the program in event dispatching thread then UI might get freeze.
So we need to do the server operation in non event dispatching thread. but we need to get back to event dispatching thread to update the button status or show the updates. Here we will need one of the methods.

Lets look at both the methods and their differences:

invokeLater : This method requests some code to be executed in the event-dispatching thread. It returns immediately, without waiting for the code to execute.


invokeAndWait : This has same behavior except for the fact that this method waits for the code to execute.

Following is the code to demonstrate both the methods :

1. Example of invokeLater :

In this following code , code will print counts irrespective of you press OK button in OK button message.


Thread reqThread = new Thread() {
public void run()
{

try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int ret = JOptionPane.showConfirmDialog(null,
"Do you wish to continue ?",
"InvokeLater Test",JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.OK_OPTION) {
System.out.println("Print OK");
}
}
});
} catch (Exception e) {
System.out.println(e.getMessage());
}
for( int i = 0; i < 10 ; i++){
System.out.println("Printing "+i);
try{
Thread.sleep( 1000 );
}catch(Exception es){

}
}
}
};
reqThread.start();


invokeAndWait example:

In this following code , code will not print counts until you press OK button in OK button message.


Thread reqThread = new Thread() {
public void run()
{

try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
int ret = JOptionPane.showConfirmDialog(null,
"Do you wish to continue ?",
"InvokeAndWait Test",JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.OK_OPTION) {
System.out.println("Print OK");
}
}
});
} catch (Exception e) {
System.out.println(e.getMessage());
}
for( int i = 0; i < 10 ; i++){
System.out.println("Printing "+i);
try{
Thread.sleep( 1000 );
}catch(Exception es){

}
}
}
};
reqThread.start();
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment