Java uses callbacks and threading to handle a whole process of time-consuming response

Source: Internet
Author: User
Tags execution thread

Now there are many processes involved in the process of lengthy and time-consuming response, such as access to WebService, remote calls, complex processing, and so on, if we use a direct sequential execution of the process may cause the interface to pause, response stop, unnecessary waiting for defects, this is not the case.

A time-consuming response process should be handled with callbacks and threads, specifically by modifying the original sequential execution to an asynchronous way and having the caller invoke the caller to get the execution result. In the example of an attachment, the viewer is the caller, which represents the interface, and the Longtimeresponse is the callee, which starts a time-consuming process with the thread and notifies the caller when execution is complete.

The viewer class code is as follows:

public class Viewer{
   private int count;
  
   public Viewer(int count){
     this.count=count;
   }
  
   public void printNewCount(int newCount){
     System.out.println("New Count="+newCount);
   }
   public int getCount() {
     return count;
   }
   public void setCount(int count) {
     this.count = count;
   }
}

The Longtimeresponse class code, as you can see, recalls the caller because it has a caller's reference viewer inside it, and the viewer is assigned a value in its constructor:

package com.sitinspring;
public class LongTimeResponse implements Runnable{
   private Viewer viewer;
   private int count;
  
   public LongTimeResponse(Viewer viewer){
     this.viewer=viewer;
     this.count=viewer.getCount();
    
     caculateNewCount();
   }
  
   private void caculateNewCount(){
     Thread thread=new Thread(this);
     thread.start();
   }
  
   public void run(){
     try{
       Thread.sleep(10000);  
     }
     catch(Exception ex){
       ex.printStackTrace();
     }
    
     viewer.printNewCount(count*count*count);
   }
}

The calling procedure is as follows:

Viewer viewer=new Viewer (10);

Longtimeresponse longtimeresponse=new longtimeresponse (Viewer);

Viewer.printnewcount (123);

It can be seen from the execution that the program first outputs

New count=123

After 10 seconds to output:

New count=1000

This shows that the program is executed asynchronously, time-consuming process does not affect the operation of the skeleton program, and time-consuming process is completed, the return results are notified to the caller, the backbone program is not affected by time-consuming process, so it will not lead to interface pauses, response stop, unnecessary waiting for defects.

These are the entire process of using callbacks and threading to handle a time-consuming response.

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.