Java Swing Multithreading Deadlock problem resolution

Source: Internet
Author: User

When developing a graphical interface based on Java swing, it is often a problem of swing multithreading. We can think about that, if you need to display a lot of data in a graphical interface, the data is obtained over a long, complex query and operation. If the graph interface in the same thread in the query and calculation work will lead to a period of time the interface is frozen, which will give users a bad interactive feeling. In order to solve this problem, we usually start a single thread to do operation and query work, and update the graphical interface at any time. At this point, another problem arises, may not only not solve the original occasional panic problem, but also may cause the program to die completely. Luckily, a shortcut key to the interrupt program is hidden in the JDK, which is ctrl+break, which Sun did not publish in the document. If you start the Java program in command-line mode and then press the Ctrl+break key, you get the trace information for the stack. From these trace information you can know where the specific cause of the panic.

When a program creates a deadlock, you will want to find the cause as quickly as possible and solve it. At this point, your general energy will be used to find the location where the deadlock is thrown, and the other half of the effort will be used to track the stack. A determination of the cause of the deadlock. But in a Java swing program, all of your efforts may be worthless. This is because Java has a special requirement for swing's multithreaded programming. In swing, GUI components can only be modified on the same line Chengri as swing.

That is, if you are going to execute a code similar to Jlabel1.settext ("BlaBla"), you must be in a swing thread, not in another thread. If you have to modify a component in another thread, you can solve it in a similar way:

SwingUtilities.invokeLater(new Runnable() {
       public void run() {
           jLabel1.setText("blabla");
      }
   }

Although the Invokelater method has a temporal delay in execution, it has virtually no effect and may be executed within milliseconds. There is also a invokeandwait method, which is almost unused unless it is specifically needed.

It is understandable to cause a refresh problem without using Invokelater, but the advantage of causing a deadlock is incredible. Luckily, it's not always necessary to invoke the change method, because most of the time we are updating the interface with the swing line Chengri. For example, the Actionlistener.actionperformed method of the Listener button click event is to run in the same thread as swing. But if another class is referenced in the callback class and is not part of the awt/swing, the result is difficult to determine. So the use of invokelater should be the safest.

Note that anything you do in Invokelater will cause the swing thread window drawing work to pause and wait for the invokelater to end. So do not take time-consuming operations in Invokelater, try to do only those interface drawing related work. Code refactoring allows you to centralize the code that is associated with the interface update in a unified approach.

One suggestion is that the classes used in swing are properly designed. Before code execution, determine if it is in a swing thread (using the Swingutilities.iseventdispatchthread () method), and if not, you need to pass Swingutilities.invokelater (Runnable ) executes, otherwise the code is executed directly. But it's easy to say, but there are a lot of difficulties in actual operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.