Chat Future mode-order cake

Source: Internet
Author: User

Introduction of the future model

The future Youdao translation:n. Futures; prospect ; future. I think it's more appropriate to use futures to explain. To give a real life example, today my girlfriend's birthday, I go to the cake shop to set a big cake for my girlfriend, super big kind.

"Boss, I want the biggest and most beautiful cake"

"Good,999 yuan, pay first."

"So expensive!" "Think of it or buy it, dig out the money, pay the bill, the boss gave me a cake ticket."

"Come and fetch in the afternoon. Take this cake ticket in the afternoon and get the cake.

"Nani, for so long."

"There is no way, you can choose to wait here, we will do it immediately, but I suggest you go to do something else, to buy a flower or something, so your girlfriend will be more happy."


One o'clock in the afternoon I went to the cake shop early.

"Boss, where's my cake?" ”

"Sorry, not yet, you wait a little while"


10 minutes later, I finally took my cake ticket to the super big cake for my girlfriend.

This is the future mode, cake is the data i want (realdata), go to the cake shop to order cake is I call to fetch data method, pay the money, I did not really get the cake, just a cake ticket (Futuredata) , to fetch the data is to take this cake ticket ( Futuredata ) Take ( Get ) Cake ( Realdata ). When you take the cake may not be ready, you still have to wait, if done, you can just take away. The advantage of this model is that you don't have to wait for the cake to come out, and you can do something else when you do the cake. One thing is that you can not accurately know how long the cake can be done, it is likely to come early, or to wait, but no relationship, is always waiting for the strong bar.

Second, the Code implementation

 Public InterfaceIcake {String getName ();} Public classRealcakeImplementsicake{ PublicRealcake () {Try {            //making cakes is a time-consuming job.Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); }    }         PublicString GetName () {return"The biggest and most delicious cake"; }} Public classCaketicketImplementsIcake {PrivateRealcake cake;  Public synchronizedString GetName () { while(cake==NULL){            Try {                //not yet ready, waiting ...wait (); } Catch(interruptedexception e) {e.printstacktrace (); }        }        returnCake.getname (); }     Public synchronized voidSetcake (realcake cake) { This. Cake =cake; //Yes, we can take the cake.Notifyall (); }} Public classCakeshop {//ways to get a cake     Public StaticIcake Getcake () {//constructs a virtual data to return to the customer, this is only a fictitious result, has given the customer a result card, but the customer is best not//hurry to get results, because the results of data generally need a certain time to construct, the customer is best to do something else, after a while to pick up        FinalCaketicket cake =NewCaketicket (); //using a thread to construct the actual data, why use a thread, because this resource construction is time-consuming        NewThread (NewRunnable () { Public voidrun () {Realcake realdata=NewRealcake ();            Cake.setcake (Realdata);        }}). Start (); returncake; }} Public classMe { Public Static voidMain (string[] args) {Icake cake=Cakeshop.getcake (); System.out.println ("Buy flowers."); //dosomethingSystem.out.println ("The name of the cake:" +cake.getname ()); }}

Java5 provides a thread with a return value to implement, the name is called the future, looked at, found that is the future mode, you can directly use it.

 Public StaticFuture<realcake>Getcake () {Executorservice ThreadPool=Executors.newcachedthreadpool (); Future<RealCake> Futuredata = Threadpool.submit (NewCallable<realcake>() {             PublicRealcake Call ()throwsException {realcake realcake=NewRealcake (); returnRealcake;        }        }); returnFuturedata; } Public Static voidMain (string[] args)throwsinterruptedexception, executionexception { future<RealCake> cake =Cakeshop4future.getcake (); System.out.println ("Buy flowers."); //dosomethingSystem.out.println ("The name of the cake:" +cake.get (). GetName ()); }

Third, the use of the scene

A request to get a map of the picture, as well as some location information, because the map image generation is time -consuming, then we use the future mode, let a generation map of the thread slowly generate pictures, and get location information can also be done at the same time, You don't have to wait until the picture is finished to get location information.

the problem with the future pattern is to let the things that were executed in order be executed simultaneously.

Chat Future mode-order cake

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.