Process a time-consuming response using a callback thread

Source: Internet
Author: User

From: http://www.blogjava.net/sitinspring/archive/2007/06/28/126809.html

Currently, the program processes a lot of long and time-consuming response processes, such as accessing WebService, remote calls, and complex processing. If we process them in direct order, it may cause the interface to pause, it is not appropriate to respond to defects such as stop and unnecessary waiting.

A time-consuming response process should adopt a callback thread. Specifically, the original sequential execution should be modified to an asynchronous method, and the caller can call the caller to obtain the execution result. In the example of the attachment, Viewer is the caller, which represents the interface, while LongTimeResponse is the caller. It starts a time-consuming process using a thread internally, and notifies the caller after the execution is completed.

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 is as follows. We can see that it can call back the caller because the caller references the viewer internally 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 );
}
}

 

The call process is as follows:

Viewer viewer = new Viewer (10 );
LongTimeResponse longTimeResponse = new LongTimeResponse (viewer );
Viewer. printNewCount (123 );

It can be seen from execution that the program outputs
New COUNT = 123
10 seconds later:
New COUNT = 1000

This indicates that the program is executed asynchronously, and the time-consuming process does not affect the running of the main program. After the time-consuming process is completed, the caller is notified of the returned results, the main program is not affected by the time-consuming process, so it will not lead to interface pauses, response stops, unnecessary waiting and other defects.

The preceding process uses the callback thread to process a time-consuming response.

Here you can download the entire program:
Http://www.blogjava.net/Files/sitinspring/Callback20070628133516.zip

 

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.