The future mode of threading design

Source: Internet
Author: User

Main

 package   Com.cmos.ngkm.web.controller.basic;  public  class   Main { public  static  void  main (string[] args) throws   interruptedexception{futureclient fc  = new   futureclient ();        Data Data  = fc.request ("Request parameter"  "Request sent successfully!        ");        System.out.println ( "Do other things ..."  = Data.getrequest ();    SYSTEM.OUT.PRINTLN (result); }}
Futureclient
 PackageCom.cmos.ngkm.web.controller.basic; Public classfutureclient { PublicData Request (FinalString querystr) {        //1. I want a proxy object (the implementation class of the data interface) to return to the client sending the request, telling him that the request has been received, and can do other things        FinalFuturedata Futuredata =NewFuturedata (); //2. Start a new thread to load the real data and pass it to the proxy object        NewThread (NewRunnable () {@Override Public voidrun () {//This new thread can slowly load the real object and pass it on to the proxy object .Realdata Realdata =NewRealdata (QUERYSTR);            Futuredata.setrealdata (Realdata);        }}). Start (); returnFuturedata; }}

Futuredata

 PackageCom.cmos.ngkm.web.controller.basic;Importgroovy.transform.Synchronized; Public classFuturedataImplementsdata{PrivateRealdata Realdata; Private BooleanIsReady =false; @Override Public synchronizedString getrequest () { while(!IsReady) {            Try{wait (); }Catch(interruptedexception e) {e.printstacktrace (); }        }        return  This. Realdata.getrequest (); }     Public synchronized voidSetrealdata (Realdata realdata) {if(isReady) {return; }         This. Realdata =Realdata; IsReady=true;    Notify (); }}

Realdata

 PackageCom.cmos.ngkm.web.controller.basic; Public classRealdataImplementsdata{PrivateString result;  PublicRealdata (String querystr) {System.out.println ("Based on" +querystr+ ", this is a time-consuming operation ..."); Try{Thread.Sleep (5000); }Catch(interruptedexception e) {e.printstacktrace (); } result= "Query Results"; } @Override PublicString getrequest () {returnresult; }}

Data

 Package Com.cmos.ngkm.web.controller.basic;  Public Interface Data {    String getrequest ();}

The future mode of threading design

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.