Java Asynchronous Operation instance

Source: Internet
Author: User
Tags throwable

1. Asynchronous Operation Process Example:

A. Opening a thread to perform time-consuming operations

B. thread.iscompleted () by the number of polling threads per multiple

C. After execution, return real information via callback function

A caller returns a pickup voucher when it calls a time-consuming operation and cannot return data immediately. And then after a break,
Get real data with the receipt voucher.

So the bridge between the link caller and the real data is the pickup voucher. Let's start by looking at its implementation:

public class futureticket{
Private Object data = null;
Private Boolean completed = false;

Public synchronized void Makerealdata () {
if (this.complited) return;
Gets the time-consuming operation of the data. Use Sleep instead
try{
Thread.Sleep (10000);
}catch (Throwable t) {}
This.data = "Data content returned";
This.completed = true;
Notifyall ();
}

Public synchronized Object GetData () {
while (!this.completed)) {
try{
Wait ();
}catch (Throwable t) {}
}
return this.data;

}
public boolean iscompleted () {
return this.completed;
}
}

To simplify the explanation (not to complicate their relationship), the OBJECTB is used instead of the real data. and the real implementation
We should put Makedata in a class of real data, and then provide a way to return real data. So for the real
Processing of data and decoupling of receipt vouchers.

For this pickup credential, the caller's invocation is the key to the asynchronous invocation:

PUBLC class requester{
Public Futureticket request () {
Final Futureticket ft = new Futureticket ();

Call a time-consuming operation in a new thread
New Thread () {
public void Run () {
Ft.makerealdata ();
}
}.start ();
return ft;
}
}
After the time-consuming operation is started in a new thread, the bill of lading is returned immediately without waiting for the thread to complete.

The caller can then invoke GetData () to get real data based on ft.iscompleted ().
Of course, the ft.iscompleted () test can be patrolled at specified intervals (very low-level scenarios) or
Wait () when the condition is not met, and then await Makedata's notifyall (); So you're done with a
Asynchronous operations modeled with Java.


Improved:
However, such a call still continues to control the threading operation for the caller. If the caller is a veteran
Programmer, of course, that's fine. But if we entrust the processing of the direct data to the receipt voucher. Call
The operator directly specifies the operation of the data, and then by the receipt of the document to invoke the specified operation, which for the caller is a very
A good relief:

Interface processdata{
public void process (onject data);
}

Public myprocessdata{
public void process (Object data) {
You don't care when the data is first retrieved.
You just have to rule out what to do if you get the data.

System.out.println (data.tostring () + "processing done .....");
INSERT INTO DataBase?
}
}

The pickup voucher knows how to handle the data obtained when the receiving caller requests the data:

public class futureticket{
Private Object data = null;
Private Boolean completed = false;
Private ProcessData PD;

Public Futureticket (ProcessData PD) {
THIS.PD = PD;
}
Public synchronized void Makerealdata (ProcessData PD) {
if (this.complited) return;
Gets the time-consuming operation of the data. Use Sleep instead
try{
Thread.Sleep (10000);
}catch (Throwable t) {}
This.data = "Data content returned";
This.completed = true;
Notifyall ();
}

Public synchronized void PutData () {
while (!this.completed)) {
try{
Wait ();
}catch (Throwable t) {}
}
return this.data;
No return, direct processing.
This.pd.process (This.data);
Alert (?);

}


This method is also not possible.
public boolean iscompleted () {
return this.completed;
}
}

Call:

Final Futureticket ft = new Futureticket (new ProcessData ());

Call a time-consuming operation in a new thread
New Thread () {
public void Run () {
Ft.makerealdata ();
}
}.start ();
Ft.putdata ();

OK, now you can smoke, drink, pick up chicks. FT will do all the work for you.

Java Asynchronous Operation instance

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.