Event dispatch thread EDT

All event processing is carried out on the Event Dispatch Thread (EDT). This type of event model is usually called a single-threaded model.

This model dictates that all access to components must be done on the EDT.

Why does access to components need to be done on the EDT? This is mainly to ensure that the changes to the component state are synchronous and to ensure the determinability of the interface components. This model is the model used by most GUI tools, including Swing/AWT, SWT, GTK, WinForm, etc.

 

The advantage of this model is that the structure design and code implementation are relatively simple, avoiding complex processing to achieve thread synchronization.

However, it also brings some problems. The most common problem is that programmers easily place the processing of long-term complex tasks in the event handler function, causing the EDT thread to be blocked and causing the user the illusion that the interface is unresponsive.

 

In fact, most of people's feelings about Swing's slow speed and slow response come from this. Simply put, it is the programmer 's problem, not Swing's own problem, because the programmer does not understand this event processing mechanism.

In fact , any tool based on this event model will appear in SWT, GTK, WinForm, etc.

The way to reproduce it is that you simply put long processing tasks in event handlers and your UI becomes unresponsive.

How to solve this kind of problem?

A common approach is to use asynchronous threads to handle long tasks .

But also remember that, in this task, the interface update should use SwingUtilities.invokeLater or the Synchronize method in SWT, and put the access operation on the EDT.

 

 Example



 

 sample code

package test;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Frame1 extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Frame1 frame = new Frame1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace ();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Frame1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		final JLabel label = new JLabel();
		contentPane.add(label, BorderLayout.CENTER);
		
		JButton btnNewButton = new JButton("按钮");
		contentPane.add(btnNewButton, BorderLayout.SOUTH);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				label.setText("Start executing...");
				
				//Time-consuming operation, start a new thread to process, but the change of appearance still needs to be executed in EDT.
				Thread thread = new Thread() {
					@Override
					public void run() {
						try {
							//update appearance
							SwingUtilities.invokeLater(new Runnable(){
	        					public void run() {
	        						label.setText("Executing time-consuming task...");
	        					}
	        				});
							
							// Simulate time-consuming operations
							Thread.sleep(5*1000);
							
							//update appearance
							SwingUtilities.invokeLater(new Runnable(){
	        					public void run() {
	        						label.setText("The time-consuming task is completed");
	        					}
	        				});
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
				};
		    	thread.start();
			}
		});
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326766382&siteId=291194637