On the Java platform, there are three roles for implementing asynchronous calls:

Caller Pick-up voucher Real data

A caller is invoking a time-consuming operation,When data cannot be returned immediately,First return a pickup document.Then after a break time, the actual data is obtained with the receipt voucher.

When calling a method,The program will enter the body of the called method,After executing this called method,Only then return to execute the next statement.How to do it like ajax asynchronous request,After sending the request,Execute the next statement without waiting for the request response. For java asynchronous requests,I found a lot of textbooks but couldn't find them.Such as thinking in java, core java2 ... and so on. Inspired by the multi-threaded download tool and request from mootools, I made a java version of request, and I do n’t know how it performs.

request:request carrier

public class request {
 private requestcontent rc;//The request body
 public request (requestcontent rc) {
 protected void start () {//Start request
 final thread t=new thread (new runnable () {
  public void run () {
  try {
   rc.dosomething ();//Respond to the request
  } catch (exception e) {
   e.printstacktrace ();
   rc.onfailure ();//If execution fails
  rc.onsuccess ();//If the execution is successful
 t.start ();

requestcontent:request body

abstract class requestcontent {
 void onsuccess () {//Perform a successful action.
Users can override this method
 system.out.println ("onsuccess");
 void onfailure () {//Execute the failed action.
Users can override this method
 system.out.println ("onfailure");
 abstract void dosomething ();//The user must implement this abstract method,Tell child threads what to do
 new request (new requestcontent () {
  void dosomething () {
  system.out.println ("dosomething");
  void onsuccess () {
  system.out.println ("override onsuccess");
 }). start ();

The above code is the asynchronous implementation of java we share with you.Hope you like it.

  • Previous ASPNET with Ajax to implement search hints
  • Next Million levels know PHP development for user data capture and analysis