Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set up two or more the event dispatch thread (EDT)?

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

I'm experimenting with setting up EDT and how it works in updating the content of a "heavy duty" panel with potentially a dozen of panels embedded inside and with hundreds of components altogether. Currently I have

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

I've looked at the following posts:

Measuring "busyness" of the event dispatching thread

How does the event dispatch thread work?

Java Event-Dispatching Thread explanation

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

and so forth.

I sort of understand that if there are, say a dozen of events, that an single EDT has to handle, Java already has an internal scheduling mechanism to group/prioritize 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 2nd EDT with new Thread(new Runnable() { ... }.start() below?

Will java automatically merge the two EDTs back to one for fear of thread safety?

       new Thread(new Runnable() {
        public void run() {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    panel.update();
                }
            });
        }
    }).start();
like image 374
mk7 Avatar asked Oct 19 '22 19:10

mk7


1 Answers

There can only be one Event Dispatch Thread!

But why would you even want to have more than one thread for this? Even for "heavy duty" panels with many components (in the application i am currently working on there must be 1000s of components) one EDT is enough. Remember you should not perform any tasks on the EDT that use a lot of CPU time. Otherwise you will block the EDT for update events and your GUI will become "sluggish" in responding to user input.

Also remember that all GUI components should be created and manipulated only from within the EDT because many components are not thread save. Ignoring this guideline may work for specific tasks but sooner or later you will get strange behavior and/or crashes!

like image 78
jo- Avatar answered Oct 22 '22 08:10

jo-