Future pattern[order Pickup mode]
One: Participants in the future pattern
--->client (customer needs)
--->host (cake shop)
--->data (interface of bills and cakes)
--->future (cake bill)
--->readata (cake making process)
II: When will the future pattern pattern be used?
--->
Three: Thinking of the future pattern
--->
Four step instructions
--->
Data class Interface
1 Packagecom.yeepay.sxf.thread9;2 /**3 * Data interface for defining the method of obtaining "cake" success4 * @authorSXF5 *6 */7 Public InterfaceData {8 9 /**Ten * How to get the cake One * @return A */ - Public AbstractString getcontent (); -}
View Code
Cake class
1 Packagecom.yeepay.sxf.thread9;2 /**3 * The real job of making cakes4 * @authorSXF5 *6 */7 Public classRealdataImplementsData {8 //the real cake9 PrivateString content;Ten One //the process of making a cake A PublicRealdata (String data,integer count) { -System.out.println ("The work of making cakes begins"); -StringBuffer buffer=NewStringBuffer (); the for(inti=0;i<count;i++){ -Buffer.append (data+count); - Try { -Thread.Sleep (1000); +}Catch(interruptedexception e) { - //TODO auto-generated Catch block + e.printstacktrace (); A } at } -Content=NewString (buffer); -System.out.println ("The end of the work of making cakes"); - - } - in /** - * Back to the made-up cake to */ + @Override - PublicString getcontent () { the //TODO auto-generated Method Stub * returncontent; $ }Panax Notoginseng - the}
View Code
Tickets for the cake
1 Packagecom.yeepay.sxf.thread9;2 /**3 * Ticket for cake collection4 * @authorSXF5 *6 */7 Public classFuturedataImplementsData {8 //the real cake9 Privaterealdata data;Ten //does the cake make good evidence? One Private Booleanflag=false; A - Public synchronized voidSetrealdata (Realdata realdata) { - if(flag) { the return; - } - This. data=Realdata; - This. flag=true; + //cake Production is successful, the thread that wakes up the cake - notify (); + } A at - - //get a cake through a ticket - @Override - Public synchronizedString getcontent () { - //TODO auto-generated Method Stub in //If the cake is not made, go to the thread of the cake to rest - while(!flag) { to Try { + wait (); -}Catch(interruptedexception e) { the //TODO auto-generated Catch block * e.printstacktrace (); $ }Panax Notoginseng } - returndata.getcontent (); the } + A the +}
View Code
Start the class that makes the cake thread
1 Packagecom.yeepay.sxf.thread9;2 /**3 * Start a new thread to make a cake4 * @authorSXF5 *6 */7 Public classHost {8 9 /**Ten * Request the standard for making cakes One * @paramCount A * @paramC - * @return - */ the PublicData Request (Final intCountFinalString c) { -SYSTEM.OUT.PRINTLN ("Request +c+" and "+count+" to make the cake "); - //the bill to build this cake - FinalFuturedata Futuredata=NewFuturedata (); + //making a cake takes time, starts a new thread, and makes a cake . - NewThread () { + A @Override at Public voidrun () { - //thread body for making cakes -Realdata Realdata=NewRealdata (c, count); - //stuffing a prepared cake into a bill - Futuredata.setrealdata (realdata); - } in - }.start (); to +System.out.println ("If the cake has been requested, give me a ticket to take the cake.")); - the returnFuturedata; * $ }Panax Notoginseng}
View Code
Simulate the main thread, first make cakes, go to busy other things, finish the class of cake
1 Packagecom.yeepay.sxf.thread9;2 /**3 * Analog Client4 * @authorSXF5 *6 */7 Public classTest {8 9 Public Static voidMain (string[] args) {TenHost host1=NewHost (); OneHost host2=NewHost (); A //send request to get cake ticket -Data data=host1.request (2, "SXF")); -Data data2=host2.request (3, "* * *"); the //I can go to other things. -System.out.println ("Host1 test.main (buy food and cook, go to the afternoon to pick up the cake)"); - - //Take the cake +String cake=data.getcontent (); -String cake2=data2.getcontent (); + //Eat Cake ASystem.out.println ("Test.main (Host1) eate" +cake); atSystem.out.println ("Test.main (Host2). Eata" +cake2); - } - -}
View Code
Multithreaded Programming Learning (9) Future pattern