Thread Synchronization of GUI code in Java swing

Source: Internet
Author: User

Gui code Thread Synchronization

The event processing and painting code of the swing component are called by one thread in sequence,
This ensures the synchronous execution of event processing.
This thread is the event-dispatching thread.

To avoid possible deadlocks, make sure that the swing components and models are created, modified, and searched.
It is called only in event-dispatching thread.

Invokelater (runnable) and invokeandwait (runnable) of swingutilities)
All add GUI code to event-dispatching thread.

Invokelater () returns immediately, while invokeandwait () blocks until event-dispatching thread
Run the specified gui code.
The invokeandwait () method may also cause deadlock. Use invokelater () whenever possible ().

About realized
Realized means that the component has been painted on screen, or is ready to be painted.
Setvisible (true) and pack () cause a window to be realized, which in turn causes the components it contains to be realized.

Synchronization will lead to waiting. When an event processing time is too long, the subsequent event processing will not be able to respond, giving people a sense of insensitivity.
The swingworker or Timer class can solve this problem,
Swingworker is a class defined by Java-tutorials, not a swing API.

SwingworkerAfter using other threads to execute time-consuming tasks, you can add GUI code to the Event-dispatching thread.
The time-consuming task is written in construct (), and the GUI code after the time-consuming task is written into finish.
Public void actionreceivmed (actionevent e ){
...
If (icon = NULL) {// haven' t viewed this photo before
LoadImage (imagedir + pic. filename, current );
} Else {
Updatephotograph (current, PIC );
}
}
...
// Load an image in a separate thread.
Private void LoadImage (final string ImagePath, final int index ){
Final swingworker worker = new swingworker (){
Imageicon = NULL;

Public object construct (){
Icon = new imageicon (geturl (ImagePath ));
Return icon; // return value not used by this program
}

// Runs on the event-dispatching thread.
Public void finished (){
Photo PIC = (photo) pictures. elementat (INDEX );
PIC. seticon (icon );
If (Index = current)
Updatephotograph (index, PIC );
}
};
Worker. Start ();
}

 

The following technologies can be used to make the swing program work smoothly:
● If you need to update components but the code is not in the event listener, use invokelater () or invokeandwait () to add code to the Event-dispacthing thread.
● If you are not sure whether your code is in the event listener, you must make it clear that the Chinese method of the program will be called by the thread. It is difficult to determine the call status of the method by the thread, but call swingutilies. iseventdispatchthread () to determine whether the thread that calls the method is event-dispatching thread. For security, you can pass all the GUI code outside the event listener to edthread through invokelater (), even if invokelater () is called in edthread (). However, calling invokeandwait () in edthread throws an exception.
● If you need to wait for a while before updating the component, create a timer to complete the task.
● If you need to update the component every time (regular interval), use timer.
* See also: How to Use swing Timers:
Http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

 

References:

* How to Use threads
Java tutorials> creating a GUI with jfc/swing> using other swing features
Http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

 

 

 

Related Article

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.