The time-consuming task in swing requires another new thread, and the update GUI operation in this new thread still needs to be done by EDT

Source: Internet
Author: User

Recently debugging the program found that the click on an interface will appear to die, the frequency of occurrence is more frequent.

Once again, when the card is dead, using JVISUALVM to see how the thread is running, the dump operation discovers that a deadlock has occurred between the threads:
Found One java-level deadlock:
=============================
"Thread-122":
Waiting to lock monitor 0x484052e4 (Object 0x1af2bb08, a com.raisecom.ems.templet.client.panel.SnmpTablePanel),
Which is held by "awt-eventqueue-0"
"Awt-eventqueue-0":
Waiting to lock monitor 0x4861c81c (Object 0x180d5950, a java.awt.component$awttreelock),
Which is held by "Thread-122"

In the thread stack, look at the source threads and methods, and find the following code:

 Public voidonselectionchanged (selectionchangedevent e) {Object source=E.getsource (); if(SourceinstanceofAbstractmenutreepanel) {   ///single thread processing displayThread thread =NewThread () { Public voidrun () {if(!"". Equals (Demarcationconfigcenterview. This. M_prover))                 RefreshConfigPanel2 (); ElseRefreshconfigpanel ();   }         }    }   };  Thread.Start (); }

The update interface in threads other than EDT requires Swingutilities.invokelater, which modifies the code:

 Public voidonselectionchanged (selectionchangedevent e) {Object source=E.getsource (); if(SourceinstanceofAbstractmenutreepanel) {   ///single thread processing displayThread thread =NewThread () { Public voidrun () {if(!"". Equals (Demarcationconfigcenterview. This. M_prover)) Swingutilities.invokelater (NewRunnable () { Public voidrun () {RefreshConfigPanel2 ();                  }      }); Else{swingutilities.invokelater (NewRunnable () { Public voidrun () {Refreshconfigpanel ();           }      });   }    }   };  Thread.Start (); }

The test did not occur when the client stuck dead.

When the Swing interface program starts, it starts 3 processes, 1, the main thread 2, the System Toolkit thread: Responsible for capturing operating system events, then converting events to swing events, and then sending them to the event dispatch thread EDT 3, Event dispatch thread (EDT): Distributing events to individual components, and is responsible for invoking the drawing method update interface
All events, such as keyboards, mouse events, are converted by the toolkit thread into swing events and then placed in the event queue EventQueue, and the eventqueue distribution mechanism is managed by EDT. Therefore, any method that modifies the state of the component should be performed in the EDT, including the construction method. This is often the case with swing, which executes long events in the EDT, making the EDT unresponsive to the update interface, which is the interface stuck, a mistake that not only the novice but also the skilled programmer will make. Therefore, you must avoid long operations in the EDT, and avoid the method is multithreaded, start another thread to handle lengthy operations, such as the operation of the database, read and write files, in the process may have to update the interface to the user to prompt, such as displaying a progress bar, an event to update the interface, However, updating the interface in a thread other than EDT is not valid, as stated earlier, to update the interface by putting an update of the interface into the EDT, but the event is executed in another thread, and to solve the problem, use a method provided by Swingutilities. Invokelater,

 Public void actionperformed (ActionEvent e) {    new Thread (new  Runnable () {            // do something            swingutilities.invokelater (new  Runnable () {                void  Run () {                    //update the GUI                }            })    . Start;}

The function of this method is to put a task of updating the interface into the EDT, and EDT will invoke it at the appropriate time to update the interface. Invokelater is responsible for creating a specific event that contains runnable and having it queued for invocation in the EDT, which runs the Run method in runnable when it is called.

Http://www.cnblogs.com/lnlvinso/p/3685863.html

The time-consuming task in swing requires a new thread, and the operation of updating the GUI in this new thread still needs to be done by EDT (GO)

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.