Is there a way to configure two or more event dispatch (EDT) streams?

Is Java capable of creating more than one EDT at a time?

I am experimenting with customizing EDT and how it works when updating the contents of a β€œheavy duty” panel with a potentially dozen panels built in and hundreds of components. I am currently

public void run() { SwingUtilities.invokeLater(new Runnable() { public void run() { panel.update(); } }); } 

I looked at the following posts:

Occupancy Flow Measurement of Event Dispatch

How does the event dispatch thread work?

Discussion of Java-Dispatching Thread Events

http://en.wiki2.org/wiki/Event_dispatching_thread

etc.

I understand that if there are, say, a dozen events that one EDT should handle, Java already has an internal planning mechanism for grouping / prioritizing these events.

According to http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

 "This is necessary because most Swing object methods are not "thread safe": invoking them from multiple threads risks thread interference or memory consistency errors." 

So what if I create a second EDT with a new thread (new Runnable () {...}. Start () below?

Will java automatically merge two EDTs back into one, fearing thread safety?

  new Thread(new Runnable() { public void run() { SwingUtilities.invokeLater(new Runnable() { public void run() { panel.update(); } }); } }).start(); 
+5
source share
2 answers

There can only be one stream of events.

But why do you want to have multiple threads for this? Even for "heavy duty" panels with many components (in the application in which I am currently working, there must be 1000 components) one EDT is enough. Remember that you should not perform any tasks on EDT that use a lot of processor time. Otherwise, you will block the EDT for update events, and your GUI will become "sluggish" when you respond to user input.

Also remember that all GUI components must be created and processed only from the EDT, because many components are not threads. Ignoring this guide may work for certain tasks, but sooner or later you will get strange behavior and / or failure!

+2
source

Swing GUI is single-threaded. This single stream is EDT. If you want to introduce a second EDT (and still work with a GUI), you will also have to rewrite a lot of Swing's internal code to take into account the added complexity of thread safety.

Adding another EDT will result in greater complexity for an unknown increase (or decrease) in performance.

+1
source

Source: https://habr.com/ru/post/1210575/


All Articles