Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

There are several threads in Swing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how many threads there are in Swing. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Seeing a lot of places talking about concurrency and multithreading in Swing, it feels like it's not as good as the Sun tutorial, so let's repeat the key here. The reason why Swing is closely related to multithreading is that if you only take sequential programming in graphical interface programming (that is, your code or tasks will be executed in turn), there will be great problems, such as you have to write a FTP client, you can not let the file download, the user interface dies there, you can neither cancel the task nor interact with the interface. So it is necessary to put time-consuming tasks, such as file downloads, in a separate thread so that users can do other things at the same time. In a nutshell, there are three types of threads in Swing:

1. Start the swimming thread or the initial thread: this thread is responsible for calling the main method, which is used in many sequential programming from the beginning. In Swing, the startup thread is responsible for very few transactions, mainly doing two things, * is to create a runnable object (Runnable Object), this runnable object task is more important, it is responsible for initializing the graphical interface, the second thing is to schedule this runnable object to another very important thread, event dispatch thread to execute. The second thing is done through SwingUtilies's invokeLater and invokeAndWait methods. Almost all the code that creates the Swing component and interacts with the Swing component is executed in the event dispatch thread.

two。 Event dispatch Swing thread: the code responsible for event handling in Swing needs to run in a specific thread, which is the event dispatch thread. Most of the code that calls the Swing method also runs in this thread. The reason is that the methods in most Swing objects are not thread-safe, so you need this specific event to dispatch threads to ensure thread safety. Of course, there are some methods in the swing object that indicate that they are thread-safe, and these methods can be called from any thread. You can think of the code running in the event dispatch thread as a series of short tasks, most of which are called event handling methods, such as the ActionListener.actionPerformed () method, and other tasks can be arranged by the program code through the SwingUtilities's invokeLater/invokeAndWait method. It is important to note that the tasks in the event dispatch thread must be short, which means that these tasks can be executed quickly, and if you find a time-consuming task, you must have made a mistake. you will find that your graphical interface is often stuck or dead. For time-consuming tasks, you need another thread, such as a worker thread (Worker Thread). The way to determine when your code is running on an event dispatch thread is simple, just use the javax.swing.SwingUtilities.isEventDispatchThread () method.

3. Working Swing thread (Worker Thread) or background thread (Background Thread): you can handle time-consuming tasks in this thread.

This is the end of this article on "there are several threads in Swing". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report