Why should I use a separate thread to display the GUI in JAVA

This simple problem bothers me. You can display the JAVA GUI application by setting the setVisible property of the setVisible frames. But in almost all the examples I found on the Internet, they use a separate thread to do the same.

They do something like this,

 SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Frame().setvisible(true); //just take the idea of this line } }); 

I did not find the difference between the two methods. But there must be a special reason why everyone does this.

Can someone explain this ... thanks!

+4
java runnable swing invokelater
source share
6 answers

The main reason for starting the application this way is that Swing components are not thread safe, so you need to guarantee which thread your graphical interface will start working with: the one called Event Dispatching Thread ( EDT ). Without doing this, you cannot be sure which stream it will start in, but, as several good commentators have noted, the main stream is guaranteed not to be EDT.

You must create, modify, or modify user interface components from the EDT. Failure to do so will result in unexpected behavior (if you're lucky) and / or dirty reviews.

Some resources that I suggest you familiarize yourself with:

You can also read. Why is my JFrame template Java application using EventQueue.invokeLater in the main method?

UPDATE

This is a blog I tried to find: P

It basically explains why it is important to synchronize your main with EDT before you start, and also describes some details about why.

It also describes why many developers make this fundamental mistake when launching their applications (basically we were told that we could, but we were never allowed ... we feel bad)

+9
source share

Since each modification that you do in the graphical interface must be performed in the event dispatch thread . Here's how AWT and SWING work .

This is because redrawing is performed in a single thread using invokeLater , you allow this thread to control it without having the potential released due to the lack of security for the Swing thread. Using this syntax, you delegate these instructions to be executed in the appopriate thread that controls the GUI elements.

+3
source share

Swing is not thread safe, and all components must be initialized in EDT . This will prevent issues like blocking.

+3
source share

Swing classes are not thread safe; they get the correctness of their thread solely from the fact that all actions on them are performed in one thread (Thread Dispatch Thread or EDT). Therefore, anytime you interact with a Swing object, it should be on EDT - and SwingUtilities.invokeLater is a good way to do this.

Without this call, if you had just called setVisible(true) from any ol thread, you would not have thread safety, and Frame might not even see the action of this method. Worse, Frame could only see some of the actions, violating internal assumptions and invariants and causing odd behavior, crashes, or deadlocks.

+3
source share

Almost any operation that invokes Swing methods should be triggered in the Swing Event Dispatch dispatch thread. invokeLater () is a way to verify that this invariant holds. Read more about it here .

Also note that the same is true for most other GUI toolkits, such as forms in .NET, MFC, and others.

+3
source share

The Java gui framework is designed as a single thread for thread safety: this method is called thread restriction. Any gui operation, for example. component creation, model creation, dispatched event, etc. therefore, they must be executed in the event sending stream (EDT). The way you describe is one way to queue operations in an EDT.

+1
source share

All Articles