In the Java platform, the role of implementing an asynchronous invocation has the following three roles:
The caller takes the real data of the delivery voucher
When a caller invokes a time-consuming operation and cannot return data immediately, a fetch voucher is returned first. Then, after a break, get the real data by picking up the voucher.
When a method is invoked, the program enters the called method body and executes the called method before returning to execute the next statement. How to do like an AJAX asynchronous request, after sending a request, did not wait for a response to execute the next statement? For the asynchronous request of Java, many textbooks were found, such as thinking in Java, Core java2 ... Wait a minute. By the multi-threaded download tool and MooTools request, I made a Java version of the request, I do not know its performance.
Request: Carrier
public class Request {
private requestcontent rc;//Request principal public demand
(Requestcontent RC) {
this.rc=rc;
}
protected void Start () {//start to request
final thread t=new thread (new Runnable () {public
void run () {
try{
rc.dosomething ()//Response Request
} catch (Exception e) {
e.printstacktrace ();
Rc.onfailure (); If execution fails
}
rc.onsuccess ();//If execution succeeds
}}
);
T.start ();
}
Requestcontent: Request Body
Abstract class Requestcontent {
void onsuccess () { //executing a successful action. The user can override this method
System.out.println ("onsuccess");
}
void OnFailure () {//Execute failed action. The user can override this method
System.out.println ("OnFailure");
}
abstract void dosomething (); The user must implement this abstract method to tell the child what the thread is going to do
}
Test: Testing
New Request (New Requestcontent () {
void dosomething () {
System.out.println ("dosomething");
}
void Onsuccess () {
System.out.println ("Override onsuccess");
}
). Start ();
The above code is a small series to share the Java implementation of the asynchronous call, I hope you like.