original:
http://today.java.net/pub/a/today/2003/10/24/swing.html?page=1
Incorrect SWING thread is one of the main reasons for slow, unresponsive and 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 thread solution
Let us start with a most common 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 threadString [] results = lookup (searchTF.getText ()); outputTA. SetText (""); for (int i = 0; i Complete code can be downloaded here You will immediately find some problems. Figure 2 shows a screenshot in the search run. Figure 2. Finding in the Swing thread Note The Go button looks pressed. This is because the ActionPerformed method notifies the button to draw as an unpubstated appearance, but has not returned. You will also find the string "abcde" to look for "ABCDE" does not appear in the text area. SearchButton_ActionPerformed Line 1 code Sets the text area to the string to find. However, pay attention to Swing heavy painting is not immediately executed. Instead, place the Heavy Picture Request to Swing Thread Processing in the Swing Event Queue. But here, we occupy the Swing thread because of the lookup processing, so it can't go back immediately. To correct these issues, let's move the lookup operation into a non-Swing thread. What we first think is to let the entire method execute in a new thread. The problem is the Swing component. The text area in this example can only be edited from the Swing thread. 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 (); Outputta .SETTEXT (""); for (int i = 0; i results [0] = lookup (searchTF.getText ()); // send runnable to the Swing thread // the runnable is queued after the // results are returnedSwingUtilities.invokeLater (new Runnable () {public void run () {/ / Now WE'RE in Swing Thread Outputta.Settext (""); for (int i = 0; i problem We attempt to enforce synchronous execution through asynchronous models - attempting to put a square bolt into a circular air. Only we try to do this, we will constantly encounter these problems. From my experience, you can tell you that these codes are difficult to read, it is difficult to maintain, and it is easy to go wrong. This looks a common problem, so there must be a standard way to solve, right? Some frameworks have been used to manage Swing complex, so let's quickly preview what they can do. A solution that can be obtained is Foxtrot