Java implementation asynchronously invokes instance code _java

Source: Internet
Author: User

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.

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.