Design Pattern: Future Mode

Source: Internet
Author: User

Http:// MoD = viewthread & tid = 294 & extra = Page % 3d1

The future model can be simply viewed Proxy Mode And Thread-per-message mode In the proxy mode, a proxy is used to replace the real object (subject) generation. The generation of the target may be time-consuming. For example, in an embedded image file, it is hoped that the program can enable the file as soon as possible and display an acceptable picture to the user. On the page that does not need to see the image, use the proxy to replace the real image loading, the proxy object loads the real image only when you really need to see the image.

In this case, the user may quickly browse the file through pages, and the image file is very large. When browsing the page number of images, it will cause the image to be loaded, as a result, the user may pause when browsing the file, so we hope that after the file is opened, there will still be a background job that will continue to load images, so that the user can quickly browse the page, the pause may be improved.

In the future mode, when a request occurs, a future object is first generated to the requesting customer. It acts like a proxy object, and at the same time, the real target object is generated, A new thread-per-message (thread-per-message) is executed continuously. After a real target object is generated, it is set to future. When the client really needs the target object, the target object is ready for extraction and use by the customer.

A simple Java program snippet demonstration may look like this:
Public future request () {
final future = New Future ();

New thread () {
Public void run () {
// The following actions may be time-consuming
realsubject subject = new realsubject ();
future. setrealsubject (subject);
}. start ();

return future;

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: 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.