in Java, Swing is a thread-insecure, single-threaded design, and the result is that swing components that will be drawn on the screen can only be accessed from the event dispatch thread. The event dispatch thread is a thread that invokes a callback method such as paint and update, or an event-handling method defined in the event listener interface, for example, the Actionperformed method in ActionListener is called in the event dispatch thread.
Swing is event-driven, so it is natural to update the visible GUI in the callback function, for example, when a button is pressed and the list of items needs to be updated, it is usually implemented in the Actionperformed method of the event listener associated with the button to update the list. It is not normal to update swing components from threads other than the event dispatch thread.
It is sometimes necessary to update swing components from a thread other than the event dispatch thread, for example, there is a time-consuming operation in actionperformed, which takes a long period to return, and it takes a long time for the button to be activated to see the list of updates. The button will remain pressed until actionperformed returns, and generally time-consuming operations should not be performed in the event-handling method, because other events are not triggered until the event is returned, and the interface is similar to the stuck condition. So it may be better to perform more time-consuming operations on separate threads, which immediately update the user interface and release the event dispatch thread to distribute other events.
The Swingutilities class provides two methods: Invokelate and invoteandwait, which queue the objects that can be run on the event dispatch thread. The Run method is called when a running object is queued at the head of the event dispatch queue. The effect is to allow an event dispatch thread to invoke any block of code in another thread.
the component can be updated only from the event dispatch thread.
1. Invokelater usage
Evenqueue.invokelater usage in Java