5

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();
Community
  • 1
  • 1
mk7
  • 153
  • 1
  • 8
  • 3
    Your second thread is not EDT, just T. ;) – Gábor Bakos Jan 07 '15 at 15:42
  • 2
    I don't think you can create an EDT. Swing creates its own EDT. You can create as many threads as you like (up to some huge limit), but they won't be EDT's. Your example code creates a `Runnable` object, but it won't run `panel.update` in your thread. Instead, this object is given to Swing, which saves it somewhere, and then eventually the EDT finds it and runs it. – ajb Jan 07 '15 at 15:43
  • possible duplicate of [Multiple Event Dispatch Threads](http://stackoverflow.com/questions/7323306/multiple-event-dispatch-threads) – Jayan Jan 07 '15 at 16:10

2 Answers2

2

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!

jo-
  • 224
  • 1
  • 9
  • 1
    There are computations that I've done within a class that's also responsible for creating a panel and components. so I suppose it's a good practice for me to separate the core logic from the GUI into different class – mk7 Jan 07 '15 at 16:07
  • Correct! In general you want to have a look at the [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) pattern.Also remember to always ensure that creation and manipulation of GUI components are executed on the EDT. Most times this is automatically the case as you call the code from within e.g. an ActionListener (which is run on the EDT) but sometimes you want to access GUI elements form a different thread an then you will have to use SwingUtilities.invokeLater or SwingUtilities.invokeAndWait. – jo- Jan 08 '15 at 09:32
1

The Swing GUI is single threaded. That single thread being the EDT. If you wanted to introduce a second EDT (and still have the GUI working) you would have to also rewrite a lot of the internal Swing code to account for the added complexity of thread safety.

Adding another EDT would introduce more complexity for an unknown amount of increase (or decrease) in performance.

Kayaman
  • 70,361
  • 5
  • 75
  • 119