Why Isn’t the Swing Toolkit multi-thread safe

After Swing components have been displayed on the screen, they should only be operated on by the event-handling thread. The event-handling thread (or just event thread) is started automatically by the Java VM when an application has a graphical interface. The event thread calls methods like paint() on Component, actionPerformed() on ActionListener, and all of the other event-handling methods.

Most of the time, modifications to Swing components are done in the event-handling methods. Because the event thread calls these methods, it is perfectly safe to directly change components in event-handling code. SimpleEvent (see below simple code) shows safe Swing code.  

SimpleEvent.java—Multithread-Safe Swing Code That Uses the Event Thread

1: import java.awt.*;
2: import java.awt.event.*;
3: import javax.swing.*;

5: public class SimpleEvent extends Object {
6:     private static void print(String msg) {
7:         String name = Thread.currentThread().getName();
8:         System.out.println(name + “: “ + msg);
9:     }
11:     public static void main(String[] args) {
12:         final JLabel label = new JLabel(“————”);
13:         JButton button = new JButton(“Click Here”);
15:         JPanel panel = new JPanel(new FlowLayout());
16:         panel.add(button);
17:         panel.add(label);
19:         button.addActionListener(new ActionListener() {
20:                 public void actionPerformed(ActionEvent e) {
21:                     print(“in actionPerformed()”);
22:                     label.setText(“CLICKED!”);
23:                 }
24:             });
26:         JFrame f = new JFrame(“SimpleEvent”);
27:         f.setContentPane(panel);
28:         f.setSize(300, 100);
29:         f.setVisible(true);
30:     }
31: }

In SimpleEvent, two threads interact with the Swing components. First, the main thread creates the components (lines 12–15), adds them to panel (lines 16–17), and creates and configures a JFrame (lines 26–29). After setVisible() is invoked by main (line 29), it is no longer safe for any thread other than the event thread to make changes to the components.

When the button is clicked, the event thread invokes the actionPerformed() method (lines 20–23). In there, it prints a message to show which thread is running the code (line 21) and changes the text for label (line 22). This code is perfectly safe because it is the event thread that ends up calling setText().

When SimpleEvent is run, the frame appears and the following output is printed to the console when the button is clicked:

AWT-EventQueue-0: in actionPerformed()

The thread named AWT-EventQueue-0 is the event thread. This is the thread that can safely make changes through methods like setText().

One of the goals for the developers of Swing was to make the toolkit as fast as possible. If the components had to be multithread-safe, there would need to be a lot of synchronized statements and methods. The extra overhead incurred acquiring and releasing locks all the time would have slowed the performance of the components. The developers made the choice for speed over safety. As a result, you need to be very careful when making modifications to Swing components that are initiated outside the event thread.

Оцените статью
Добавить комментарий

Your email address will not be published. Required fields are marked *

  1. Fabrizio Giudici

    Correct, but there are two points that should be added. The most important, is that starting from some Java version the threading rule has gotten even stricter – you should even create Swing components in the event thread – that is, you should wrap the Swing stuff in the main() into an EventQueue.invokeAndWait(). Second, while it’s true that, being Swing born ten years ago, the synchronized stuff was a performance hit, there are more general reasons. Brian Goetz (“Java concurrency in practice”) at a certain point recalls that almost *all* UI frameworks are single threaded, including those in native machine code. The reason is that research has shown that multi-threaded UI frameworks would be prone to dead locking.

  2. xXmikeMomXx

    I’m trying to open forum but sometimes there are no images on it 🙁

  3. grerceito

    Hello folks,

    I am looking astral projection techniques, if possible easy and with rapid results.
    Would any of you know any or could recommend a trustworthy website?


    1. admin автор

      Do you think this website is useful? anyway you can google your keywords maybe you will found any nice…

  4. DaymnJeamwege

    Hi Iam Prabhu from chennai,joined today in this forum… 🙂

  5. Kawa

    Hiya i’m for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you aided me.