Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain what the following code does?

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        new NewJFrame().setVisible(true);
    }
});

Please tell me what does the above code does actually. I am looking for line by line explanation. especially the first line and tell me why do we use that and in what scenarios we have to use this.

like image 618
Deepak Avatar asked Feb 05 '11 18:02

Deepak


People also ask

What does following a code mean?

What is a Hospital Code Status? All patients who are admitted to a hospital are assigned a code status. As stated above, “Code Status” essentially means the type of emergent treatment a person would or would not receive if their heart or breathing were to stop.

What are the 4 types of code?

There are hundreds of coding languages in existence today. While the names of the coding paradigms sometimes vary, most experts agree on four primary types of code: imperative, functional, logical, and object-oriented.

What does the following code snippet does?

A code Snippet is a programming term that refers to a small portion of re-usable source code, machine code, or text. Snippets help programmers reduce the time it takes to type in repetitive information while coding. Code Snippets are a feature on most text editors, code editors, and IDEs.

What is code explain its types?

In programming, code (noun) is a term used for both the statements written in a particular programming language - the source code , and a term for the source code after it has been processed by a compiler and made ready to run in the computer - the object code . 1.BCD code. 2.EBCDIC code.


2 Answers

In this Example you see an anyonmous class that derives from Runnable. This anonymous class overrides the run method of the interface runnable. Then this anonymous class is instantiated and passed to the EventQueue.invokeLater method, which is a static method. This method appends the object into... well... the eventQueue. In the EvenQueue are many events, like keyboard events or mouse events or whatever. There is a Thread that continuesly polls data from this queue. Once that Thread reaches the anonymous class that was instantiated here, it will execute the run() method, which will instantiate an Object of class NewJFrame and set it to be visible.

The whole point of doing this this complicated is that the new JFrame().setVisible(true) part is not executed in the main thread, but in the event dispatching thread. In Swing you must execute all code that modifies the user interface in the event dispatching thread.

like image 135
yankee Avatar answered Sep 21 '22 20:09

yankee


Single-Thread-Model and EDT

Most modern UI libraries adopt the single-thread-model. That means, all the manipulation upon UI components MUST be done on the same single thread. Why? That's because allowing UI components being updated from multiple threads will lead to chaos since most Swing object methods are not "thread safe". For simplicity, efficiency and robustness, single-thread-model is adopted.

In Swing, the very thread that serve the single-thread-model is called the Event Dispatching Thread, i.e. EDT. It is not provided by Swing. It is provided by Abstract Window Toolkit, i.e. AWT.

Worker thread vs UI thread

A non-trivial GUI application usually has many threads. In modern GUI application, there can be many worker threads to do dirty work, but there's only one UI thread (Swing calls it EDT) to update the GUI. Worker threads usually need to reflect their work progress in GUI, so they need to communicate with the UI thread about that. So how does this communication happen?

java.awt.EventQueue

The communication happens through a message queue model. The java.awt.EventQueue is the very class that provides a event queue globally. This global event queue serves as the communication channel to the EDT. EDT picks up messages from this EventQueue and update UI components accordingly. If some other part of your program wants to manipulate the UI, that part of code should call EventQueue.invokeLater() or EventQueue.invokeAndWait() to queue a message into EventQueue. EDT will process all the pending messages in the EventQueue and eventually get to the message.

the main thread

Your code snippet usually resides in the main() thread, the main thread can be viewed as some kind of a worker thread here. Only that instead of updating the GUI by posting messages to EventQueue, it initiates the GUI. Anyway, initiation can be viewed as a kind of work, too.

After the GUI is initiated, the main thread will exits and the EDT will prevent the process from exiting.

And another good explanation:

Java Event-Dispatching Thread explanation

An interesting article: Multi-threaded toolkit, a failed dream?

like image 36
smwikipedia Avatar answered Sep 20 '22 20:09

smwikipedia