What event is the thread sending?

I know what “stream” means, and if I understand the event dispatch (EDT) stream as “just a stream,” it explains a lot, but it doesn't seem to explain everything.

I do not understand what is special about this topic. For example, I don’t understand why do we need to start a graphical interface in EDT? Why is the "main" thread a bed for a GUI? Well, if we just don’t want to occupy the main thread, why can't we only run the GUI in a “different thread”, why should it be some kind of “special” thread called EDT?

Then I do not understand why we can not start EDT, like any other thread? Why should we use some special tool (called invokeLater ). And why the GUI, unlike any other thread, does not start immediately. We must wait until he is ready to accept our work. Is it because the EDT can possibly perform several tasks at once?

If you decide to answer this question, could you use really simple terminology, because otherwise, I am afraid, I will not be able to understand the answer.

ADDED:

I always thought that we have one “task” for each thread. Thus, in each thread, we execute a predefined sequence of commands. But it seems to me that in the case of sending a stream, we may have a problem. Well, they are not executed at the same time (threads switch between different tasks, but in one thread there is another task). It is right? For example, in EDT there is one thread that displays the main window, and then in addition to this, we sent another task to EDT, which should update one of the components of the window, and EDT will execute this new task whenever it is ready. So is EDT different from other threads?

+6
java multithreading user-interface
source share
4 answers

an event dispatch thread is a thread that processes all GUI events and controls your Swing GUI. It starts somewhere in the Swing code if you have any GUI in your program. The reason this is done behind the scenes is due to simplicity - you don’t have to worry about starting and managing the additional thread yourself.

Regarding the fact that you need to update your GUI with invokeLater() , this is due to concurrency issues. The GUI can only be changed from one thread, since Swing is not thread safe (it is worth noting that most toolkits are not thread safe, there is a good article that gives some ideas why). This is why you must send all GUI updates to run on EDT.

You can read more about concurrency in the Swing and event dispatch section in the Sun Concurrency Tutorial in Swing . Also, if you want to see how this can be done differently, you may need to check out the SWT toolkit . In SWT, you must manage EDT yourself.

+6
source share

I always thought that we have one “task” for each thread. So, in each thread, we execute a predefined sequence of commands. But it seems to me that in the thread sending events, we can have a task. Well, they are not running at the same time (the thread switches between different tasks, but there is another task in one thread). It is right? For example, in EDT there is one thread that displays the main window, and then in addition we sent it to EDT - another task that should be updated by one of the window components and EDT will perform this new task whenever it is ready. Is EDT different from other threads this way?

No, EDT is not fundamentally different from other threads. And “task” is not a good word to use, because it can be confused with processes at the OS level (which are also often called task). It is better to use Runnable , the interface used to pass code to EDT for execution through invokeLater() .

EDT is mainly related to the queue of things that it should do. When a user clicks a button in the GUI, Runnable , which notifies all listeners attached to the button, enters the queue. When the window is resized, the queue goes into Runnable execute revalidate & repaint. And when you use invokeLater() , your Runnable goes into the queue.

The EDT simply starts an endless loop that says: "Take the Runnable from the queue (and if it's empty sleep mode until you are notified about it) and execute it.

Thus, it executes all these little Runnable code snippets one by one, so that each of them basically has a GUI for itself during its launch, and it does not need to worry about synchronizing anything. When you manipulate the GUI from another thread, this assumption is violated, and you may get the GUI in a damaged state.

+6
source share
 What is the EDT? 

This is a hacky workaround around the many concurrency problems that the Swing API has;)

Seriously, many Swing components are not "thread safe" (some well-known programmers have come up with a call to Swing "thread invile"). With a unique chain in which all updates are added to these stream-independent components, you shy away from many potential concurrency issues. In addition, you also guarantee that it must run Runnable, which you pass through it using invokeLater in sequential order.

Note that this is not just that you shy away from the concurrency problem: you should follow Sun's recommendations on what should and should not be done in the EDT, or you will have serious problems in your application.

Another advantage is that some Swing components tend to throw unwanted exceptions, and when this happens, they are automatically processed and will not crash EDT (AFAIK, if you really manage to kill EDT, it automatically resumes).

In other words: you don’t have to deal with all the broken Swing components and the exceptions that they throw themselves: EDT takes care of this (just look at the countless Swing errors throwing exceptions in the Sun parade, it's fun ... And yet, most applications continue work fine).

In addition, doing only what is mandatory in EDT allows the graphical interface of your application to remain “responsive” even hard, there may be tasks running in the background.

+3
source share

It is important to remember that Swing classes are not thread safe. This means that you should always call Swing methods from a single thread, or you run the risk of getting weird or undefined behavior.

So the solution: call Swing methods from only one thread. This is an EDT stream — it is nothing special, except that it is a stream for invoking swing methods.

You may now ask why Swing methods are not thread safe? After several unsuccessful attempts, the designers of the GUI toolkit found that it was initially impossible to create a thread-safe GUI toolkit. Too often, events are sent in opposite directions (input events from the bottom up, application events from the top down), which always lead to deadlocks. So the way it is.

+3
source share

All Articles