top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

In Java when should the method invokeLater() be used?

+2 votes
453 views
In Java when should the method invokeLater() be used?
posted Sep 5, 2013 by Arvind Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Method invokeLater(Runnable doRun) causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed.
This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object pickHighBetaStock on the event dispatching thread and then prints a message.

Runnable pickHighBetaStock = new Runnable() {
    public void run() {
        System.out.println("High beta Stockpicked by  " + Thread.currentThread());
    }
};
SwingUtilities.invokeLater(pickHighBetaStock);
 System.out.println("This might well be displayed before the other message. if Event-dispatcher thread is busy");
answer Sep 5, 2013 by Satyabrata Mahapatra
...