如果主线程向别的线程发送请求,别的线程处理完请求后还要返回给主线程一个结果,那么这种状况下应该使用Future Pattern。
示例:Host类中的request方法发送请求,这个方法中启动了一个线程,线程中首先建立实例RealData,RealData的构造中显示一组数据(耗时),然后设置一个属性content;request中的线程再将一个FutureData实例(Host的属性)future的属性realdata的值改为刚建立的RealData;最后request方法返回future。在Main中,要想通过future获取realdata的content,必须等到future设置了realdata以后才可以获取。所以,Main中让host调用request方法后,request建立realdata到设置future的RealData域为realdata的过程中,Main已经获得了future,但future的内容并没有改变,这是Main获取内容的时候就会等待,当Host中的线程跑完后,Main已经获得的future就发生了改变,这时Main又被激活,继续执行。
public interface Data { public abstract String getContent(); }
public class RealData implements Data{ private final String content; public RealData(int count,char c){ System.out.println(" making realdata("+count+" , "+c+")Begin"); char[] buffer=new char[count]; for(int i=0;i<count;i++){ buffer[i]=c; try{ Thread.sleep(300); }catch(InterruptedException e){ e.printStackTrace(); } } System.out.println(" making realdata("+count+" , "+c+")End"); content=new String(buffer); } @Override public String getContent() { return content; } }
RealData是建立一个真实数据的过程。
public class FutureData implements Data{ private RealData realdata=null; private boolean ready=false;//设置数据后才可以获取内容,并且只能设置一次数据 public synchronized void setRealData(RealData realdata){ if(ready){ return; //balk } this.realdata=realdata; this.ready=true; notifyAll(); } @Override public synchronized String getContent() { while(!ready){ try{ wait(); }catch(InterruptedException e){ e.printStackTrace(); } } return realdata.getContent(); } }
FutureData中有一个RealData的实例域,它获取的数据就是这个域中的内容。
public class FutureData implements Data{ private RealData realdata=null; private boolean ready=false;//设置数据后才可以获取内容,并且只能设置一次数据 public synchronized void setRealData(RealData realdata){ if(ready){ return; //balk } this.realdata=realdata; this.ready=true; notifyAll(); } @Override public synchronized String getContent() { while(!ready){ try{ wait(); }catch(InterruptedException e){ e.printStackTrace(); } } return realdata.getContent(); } }
public class Host { public Data request(final int count,final char c){ System.out.println(" request("+count+" ,"+c+")Begin"); final FutureData future=new FutureData(); //构建FutureData实例 new Thread(){ public void run(){ RealData realdata=new RealData(count ,c);//构建realdata,这是一个耗时的过程 future.setRealData(realdata); } }.start(); System.out.println(" request("+count+" ,"+c+")End"); return future; } }
public class Main { public static void main(String[] args){ System.out.println("Main Begin"); Host host=new Host(); Data data1=host.request(10,'A'); Data data2=host.request(20,'B'); Data data3=host.request(30,'C'); System.out.println("Main otherJob Begin"); //try { // Thread.sleep(2000); //} catch (InterruptedException e) { // e.printStackTrace(); //} System.out.println("data1= "+data1.getContent());//主线程到这里需等待,直到data1(future)设置了realdata System.out.println("data2= "+data2.getContent()); System.out.println("data3= "+data3.getContent()); System.out.println("Main End"); } }
运行结果:
Main Begin
request(10 ,A)Begin
request(10 ,A)End
request(20 ,B)Begin
request(20 ,B)End
request(30 ,C)Begin
request(30 ,C)End
Main otherJob Begin
making realdata(10 , A)Begin
making realdata(30 , C)Begin
making realdata(20 , B)Begin
making realdata(10 , A)End
data1= AAAAAAAAAA
making realdata(20 , B)End
data2= BBBBBBBBBBBBBBBBBBBB
making realdata(30 , C)End
data3= CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Main End
相关推荐
对结城浩的Future Pattern的代码做了一点儿小小的改进(见blog)。
6. **并发编程模式**:介绍如双检锁/双重校验锁(DCL)、生产者消费者模式、读写锁、未来的值(Future Pattern)等并发编程模式,帮助开发者解决实际问题。 7. **线程通信**:讲解wait()、notify()和notifyAll()...
- Potential future applications and implications for the field of pattern recognition. #### Conclusion *Pattern Recognition Using Neural and Functional Networks* offers a detailed exploration of ...
10. **第8章 - 模式的过去、现在与未来**(Chapter 8 - The Past, Present, and Future of Patterns): - 分析了设计模式的发展历程、当前状态以及未来趋势。这部分内容有助于读者了解设计模式的历史背景及其在...
Search Engine Indexing, PageRank, Public Key Cryptography, Error-Correcting Codes, Pattern Recognition, Data Compression, Databases, Digital Signatures, What is Computable? 计算机和非计算机人士都可以...
英文 pdf 参考资料 International Conference, SIP 2009 Held as Part of the Future Generation Information Technology Conference, FGIT 2009 Jeju Island, Korea, December 10-12, 2009 Proceedings
As future generation information technology (FGIT) becomes specialized and fragmented, it is easy to lose sight that many topics in FGIT have common threads and, because of this, advances in one ...
良葛格的《Design Pattern学习笔记》不仅涵盖了经典的GOF设计模式,还额外介绍了几种多线程模式,这使得这份学习笔记成为了一个宝贵的学习资源。下面将对其中的部分设计模式进行详细介绍。 #### 二、GOF设计模式 ...
The comprehensive nature of the survey makes it a valuable resource for anyone interested in understanding the current state and future prospects of thinning methodologies in the realm of pattern ...
Hub(枢纽,或转运中心)允许通过货物集散和中转,降低网络连接的数量,从而构建大的运输网络,即所谓的Hub-and-spoke(轴辐式网络),该组网方式能够简化网络复杂性,降低建设成本,并集中分拣和处理货物,并整合流量...
常见的特征提取方法包括Haar特征、LBP(Local Binary Pattern)、HOG(Histogram of Oriented Gradients)等。这些特征能够捕捉到图像中的局部信息,为后续的分类提供支持。 ##### 3.2 Boosting算法 Boosting算法是一...
methods developed to deal with problems of spatial pattern recognition, spatial autocorrelation, and spatial heterogeneity have seen greatly increased adoption, in part due to the availability of user...
I will use this text as a resource in future cloud designs and architectural considerations.” –Dr. Nancy M. Landreville, CEO/CISO, NML Computer Consulting The authors address topics covering ...
The book also includes examples of actor application types and two primary patterns of actor usage, the Extra Pattern and Cameo Pattern., Allen, the Director of Consulting for Typesafe—creator of ...
A sample application used throughout the book is an enterprise level ASP.NET website with multi-tiered, SOA design techniques that can be applied to your future ASP.NET projects. Read about each ...
These packages feature a predefined ball-out pattern and a straightforward addressing scheme that simplifies the design process for future memory upgrades. The use of FBGA packages ensures ...
-Changed: Enhanced the "Automatic license update" pattern to ensure it will not clash with something else in future releases. 2.70U2 KCNTRL(Control): -New: Added a new patch for Kerio Control 7.4 ...