Java Invokelater and Invokeandwait

Source: Internet
Author: User

Introduction to Invokelater and invokeandwait in Swingutilities
In Java, Swing is a thread-insecure, single-threaded design, and the result is that swing components that will be drawn on the screen can only be accessed from the event dispatch thread. The event dispatch thread is a thread that invokes a callback method such as paint and update, or an event-handling method defined in the event listener interface, for example, the Actionperformed method in ActionListener is called in the event dispatch thread.
Swing is event-driven, so it is natural to update the visible GUI in the callback function, for example, when a button is pressed and the list of items needs to be updated, it is usually implemented in the Actionperformed method of the event listener associated with the button to update the list. It is not normal to update swing components from threads other than the event dispatch thread.
It is sometimes necessary to update swing components from a thread other than the event dispatch thread, for example, there is a time-consuming operation in actionperformed, which takes a long period to return, and it takes a long time for the button to be activated to see the list of updates. The button will remain pressed until the actionperformed returns, and generally time-consuming operations should not be performed in the event-handling method, because other events are not triggered until the event is returned, and the interface is similar to the stuck condition. So it may be better to perform more time-consuming operations on separate threads, which immediately update the user interface and release the event dispatch thread to distribute other events.
The Swingutilities class provides two methods: Invokelate and invoteandwait, which queue the objects that can be run on the event dispatch thread. The Run method is called when a running object is queued at the head of the event dispatch queue. The effect is to allow an event dispatch thread to invoke any block of code in another thread.
The component can be updated only from the event dispatch thread.
Program Example: Error methods for updating components
Startbutton.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Getinfothread t = new Getinfothread (test.this);
T.start ();
Startbutton.setenabled (FALSE);
}
});

Class Getinfothread extends Thread {
Test applet;

Public Getinfothread (Test applet) {
This.applet = applet;
}

public void Run () {
while (true) {
try {
Thread.Sleep (500);
Applet.getprogressbar (). SetValue (Math.random () * 100);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
Error analysis: In actionperformed, the listener sets the allowed state of the button to false, because it is called actionperformed on the event dispatch thread, so setenabled is a valid operation, However, setting the progress bar in Getinfothread is a risky practice because threads other than the event dispatch thread update the progress bar, so running is not normal.

1, Invokelater use
Class Getinfothread extends Thread {
Test applet;

Runnable Runx;

int value;

Public Getinfothread (Final Test applet) {
This.applet = applet;
Runx = new Runnable () {
public void Run () {
JProgressBar JPB = Applet.getprogressbar ();
Jpb.setvalue (value);
}
};
}

public void Run () {
while (true) {
try {
Thread.Sleep (500);
Value = (int) (Math.random () * 100);
System.out.println (value);
Swingutilities.invokelater (RUNX);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}

2, Invokeandwait
Like Invoikelater, invokeandwait also queues the operational objects into the queue for the event dispatch thread, Invokelater returns when the operational objects are placed in the queue. Invokeandwait has been waiting to know that a running run method has been started to return. The Invokeandwait method is useful if an operation must obtain information from a component before another operation executes.
Class Getinfothread extends Thread {
Runnable Getvalue,setvalue;
int value,currentvalue;
Public Getinfothread (Final Test applet) {
Getvalue=new Runnable () {
public void Run () {
JProgressBar Pb=applet.getprogressbar ();
Currentvalue=pb.getvalue ();
}
};
Setvalue=new Runnable () {
public void Run () {
JProgressBar Pb=applet.getprogressbar ();
Pb.setvalue (value);
}
}
}
public void Run () {
while (true) {
try{
Thread.currentthead (). Sleep (500);
value= (int) (Math.random () *100);
try{
Swingutilities.invokeandwait (GetValue)///until the Run method of GetValue is returned.
}catch (Exception ex) {
}
if (Currentvalue!=value) {
Swingutilities.invokelater (SetValue);
}
}
}catch (Exception ex) {
}
}
}
An important difference between invokelater and invoikeandwait: You can call Invokelater from the event dispatch thread, but you cannot call invokeandwait from the event dispatch thread. The problem with calling Invokeandwait from an event dispatch thread is that invokeandwait locks the thread that invokes it until the running object is dispatched from the event dispatch thread and the Run method of the running object is activated. If Invoikeandwait is called from an event dispatch thread, a deadlock condition occurs because invokeandwait is waiting for the event to be dispatched, but because the invokeandwait is called from the event dispatch thread, So the event will not be distributed until invokeandwait returns.
Ex) actionperformed (); When the event dispatch thread is returned, the thread can be distributed, and using invokeandwait in actionperformed causes the actionperformed to fail to return. So it is impossible to distribute the threads in invokeandwait.

Because swing is thread insecure, it is not safe to access swing components from threads other than the event dispatch thread, and the Swingutilities class provides both methods for executing the code in the event dispatch thread

Java Invokelater and Invokeandwait

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.