Revisions for Swing threads (on)

zhaozj2021-02-16  58

Revisor of SWING thread (on) SRX81 translation (Participation: 240, Expert): 1990) Published: 2003-11-5 11:22 Updated: 2003-11-6 9:31 am Version: 1.0 Read: 1773 Secondary

By Jonathan Simon10 / 24/2003 Original: http://today.java.net/pub/a/today/2003/10/24/swing.html? Page = 1 Incorrect SWING thread is slow, no response One of the main reasons for unstable Swing applications. This is caused by many reasons, from the developer's misunderstanding of the Swing single-threaded model to ensure the correct thread execution. Even for a lot of effort to Swing threads, the application thread logic is also difficult to understand and maintain. This article describes how to use event-driven programming in developing Swing applications to greatly streamline development, maintenance, and provide high spirituality. Background Since we are to simplify the thread of Swing applications, let's take a look at how the Swing thread works, why it is necessary. Swing API is designed around a single thread model. This means that the SWING component must always modify and manipulate through the same thread. Why use a single-threaded model, which has many reasons, including development costs and synchronous Swing complexity - this will cause a dull API. In order to achieve a single-threaded model, there is a dedicated thread for interacting with the Swing component. This thread is a familiar SWING thread, AWT (sometimes pronounces "an" ought ") thread, or event assignment thread. In the following part of this article, I choose a swings thread called. Since the Swing thread is a unique thread that interacts with the Swing component, it has been given a lot of responsibility. All drawing and graphics, mouse events, component events, buttons events, and all other events occur in Swing threads. Because Swing threads have been very heavy, there is a problem when too many other work is processed in a swing thread. The most common location that will cause this problem is where non-SWING processing, like the ActionListener, such as JButton, in an event listener method, database lookup. Since the ActionListener's actionPerformed () method is automatically executed in the Swing thread, the database lookup will also be executed in the Swing thread. This will occupy Swing's work and prevent it from handling other tasks - image, respond to mouse movement, processing buttons events, and application scaling. Users think that it is dead, but actually is not the case. Executing the code in the appropriate thread is very important to ensure that the system is implemented normally. Since we have seen how important the code to execute Swing applications in the appropriate thread is now how we do these threads. Let's take a look at the standard mechanism to put the code and remove the SWING thread. In the process of telling, I will highlight several problems and difficulties related to standard mechanisms. As we see, most of the issues come from code models that try to achieve synchronization on an asynchronous Swing thread model. From there, we will see how to modify our example to event-driven - transplant the entire way to asynchronous models. General Swing threads Solution Let us start with a most commonly used Swing thread error. We will try to use standard technologies to fix this problem. In this process, we will see the complexity and common difficulties of achieving the correct swings thread. Also, pay attention to the correction of this Swing thread problem, many intermediate examples cannot work. In the example, I started with // broker where the code failed. Ok, now, let us enter our example. Suppose we are performing book findings. We have a simple user interface, including a lookup domain, a lookup button, and an output text area. This interface is shown in Figure 1. Don't criticize my UI design, this is really ugly, I admit.

Figure 1. Basic query user interface user input book title, authors, or other conditions, then display a list of results. The following code example demonstrates that the ActionListener of the button calls the lookup () method in the same thread. In these examples, I used Thread.Sleep () to sleep for 5 seconds as a placement outside. The result of thread sleep is equivalent to a time-consuming 5 second synchronization server call. private void searchButton_actionPerformed () {outputTA.setText ( "Searching for:" searchTF.getText ()); // Broken !! Too much work in the Swing thread String [] results = lookup (searchTF.getText ()); outputTA .SETTEXT (""); for (int i = 0; i

The following is a modified method searchButton_actionPerformed: private void searchButton_actionPerformed () {outputTA.setText ( "Searching for:" searchTF.getText ()); // the String [] [] is used to allow access to // setting the results From an inner class final string [] results = new string [1] [1]; new thread () {public void run () {results [0] = lookup (searchtf.gettext ());}} .start ());}} .start () (); ""); For (int i = 0; i

Below is a corrected code: private void search button_ActionPerformed () {Outputta.Settext ("Searching for:" SearchTf.getText ()); final string [] [] results = new string [1] [1]; new thread ) {public void run () {// get results results [0] = lookup (searchTF.getText (.)); // send runnable to the Swing thread // the runnable is queued after the // results are returned SwingUtilities. Invokelater (New Runnable () {public void run () {// now We're in the swing thread output (""); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-14415.html

New Post(0)