1

Can anyone explain me this piece of code in simple words:

SwingUtilities.invokeLater(
    new Runnable(){
        public void run(){
            chatWindow.append(text);
        }
    });
}

Please, can anyone give me a detail explanation.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
cool joey
  • 167
  • 1
  • 1
  • 7

6 Answers6

10

You are creating an instance of an anonymous implementation of the Runnable interface and passing it to invokeLater, which will put it on a queue. Another thread, the Event Dispatch Thread, pops Runnables off that queue and invokes their run method. (Note that this is indeed a simplistic explanation and does not correspond 100% with reality.)

In terms of the effect you as the programmer are interested in, this makes the following line of code:

chatWindow.append(text);

to be executed not on the current thread, but on the Event Dispatch Thread (EDT). If you tried to execute it on your current thread, it would result in undefined behavior because Swing is not thread-safe and all GUI operations must happen on the mentioned EDT.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • You have to append the text to chatWindow through Event Dispatch Thread because Swing works on Single Threaded Model. All the updates to the UI related things should be done through Event Dispatch Thread. – Ameer Tamboli Mar 03 '14 at 13:18
1

That simply adds a task to the tasks queue of the Java thread which is in charge of the rendering & events for your whole application.

Note : This thread is called EDT (Event Dispatching Thread).

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
0

Runnable is an interface in Java representing a type that defines a run method. A class that implements Runnable interface must provide the implementation for it. Thread is a well-known Runnable.

From the docs:

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 doHelloWorld on the event dispatching thread and then prints a message.

If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

It's a way to pass a certain amount of instructions to the AWT Event Dispatch Thread (EDT). This is needed, because it's bad practice to operate on AWT-related things (like JFrames and all of it's children) from other Threads. You better give it to the EDT and let it handle it at the appropriate moment.

The code itself uses an anonymous class that implements Runnable in order to let you specify what you want to execute on the EDT. The Runnable interfaces requires you to implement the run() method. Then the EDT will call on your class object run() when it wants to.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

The answer: http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29

Basically, it launches the code in the run() method of the in-line Runnable object as soon as all AWT events are consumed.

Alxe
  • 381
  • 3
  • 12
0

Though few good answers and explanations are here on these links but still i am adding some details:

Should we use EventQueue.invokeLater for any GUI update in a Java desktop application?

http://www.javamex.com/tutorials/threads/invokelater.shtml

Swing event handling code runs on a special thread known as the Event Dispatch Thread(EDT). Most code that invokes Swing methods also runs on this thread. This is necessary because most Swing object methods are not thread safe. All GUI related task, any update should be made to GUI while painting process must happen on the EDT, which involves wrapping the request in an event and processing it onto the EventQueue. Then the event are dispatched from the same queue in the one by one in order they en-queued, FIRST IN FIRST OUT. That is, if Event A is enqueued to the EventQueue before Event B then event B will not be dispatched before event A.

SwingUtilities class has two useful function to help with GUI rendering task:

1) invokeLater(Runnable):Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread(EDT). This will happen after all pending AWT events have been processed, as is described above.

2) invokeAndWait(Runnable): It has the same function as the invokeLater but it differs from invokeLater for the fact that: --> invokeAndWait waits for the task given by it to the EDT, to finish before returning. --> it blocks(awaits) the current(i.e., it's invoking thread from continuing it's execution by sending to WAIT state by means of synchronizing a lock. --> It will release the lock once the event request posted by this function gets dispatched in the EDT and the invoker thread of this function can continue.

Community
  • 1
  • 1
Mak
  • 596
  • 5
  • 10