Future模式可以简单的看成是 Proxy 模式 与 Thread-Per-Message 模式 的结合,在Proxy模式中,用一个Proxy来代替真正的目标(Subject)生成,目标的生成可能是费时的,例如在开启一个内嵌图片的文件中,希望程式能尽快完成开启文件的动作,并显示一个可接受的画面给使用者看,在还不需要看到图片的页面中先使用Proxy代替真正的图片载入,只有在真正需要看到图片时,才由Proxy物件载入真正的图片。
考虑这样一个情况,使用者可能快速翻页浏览文件中,而图片档案很大,如此在浏览到有图片的页数时,就会导致图片的载入,因而造成使用者浏览文件时会有停顿的现象,所以我们希望在文件开启之后,仍有一个背景作业持续载入图片,如此使用者在快速浏览页面时,所造成的停顿可以获得改善。
Future模式在请求发生时,会先产生一个Future物件给发出请求的客户,它的作用就像是Proxy物件,而同时间,真正的目标物件之生成,由一个新的执行绪持续进行(即Thread-Per-Message),真正的目标物件生成之后,将之设定至Future之中,而当客户端真正需要目标物件时,目标物件也已经准备好,可以让客户提取使用。
一个简单的Java程式片段示范可能像是这样:
....
public Future request() {
final Future future = new Future();
new Thread() {
public void run() {
// 下面这个动作可能是耗时的
RealSubject subject = new RealSubject();
future.setRealSubject(subject);
}
}.start();
return future;
}
分享到:
相关推荐
良葛格的《Design Pattern学习笔记》不仅涵盖了经典的GOF设计模式,还额外介绍了几种多线程模式,这使得这份学习笔记成为了一个宝贵的学习资源。下面将对其中的部分设计模式进行详细介绍。 #### 二、GOF设计模式 ...
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 ...
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 ...
Node.js Essential Patterns, introduces the first steps towards asynchronous coding and design patterns with Node.js discussing and comparing callbacks and the event emitter (observer pattern)....
With Learning JavaScript Design Patterns, you’ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to...
Hub(枢纽,或转运中心)允许通过货物集散和中转,降低网络连接的数量,从而构建大的运输网络,即所谓的Hub-and-spoke(轴辐式网络),该组网方式能够简化网络复杂性,降低建设成本,并集中分拣和处理货物,并整合流量...
首先,让我们来看看"currency design pattern"。设计模式是软件设计中的一种最佳实践,是解决特定问题的模板。在货币设计模式中,重点可能在于确保货币操作的正确性和一致性。这通常涉及到货币的表示(例如,使用不...
在"DesignPattern2ThreadCallback"这个主题中,我们可以深入学习如何在实际项目中结合设计模式和线程回调,以构建高效、可维护的多线程应用程序。通过理解并熟练运用这些概念,开发者能够编写出更加灵活、可扩展的...
- Potential future applications and implications for the field of pattern recognition. #### Conclusion *Pattern Recognition Using Neural and Functional Networks* offers a detailed exploration of ...
保护性暂停模式(Guarded Suspension Design Pattern)是一种在多线程环境下实现线程之间安全地传递信息的模式。这种模式主要解决了在并发编程中如何安全高效地进行线程间通信和数据传递的问题。 该模式的基本概念...
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 ...
Find out how the module design pattern is used in OOP in JavaScript Design and augment modules using both tight augmentation and loose augmentation Extend the capabilities of modules by creating sub-...
此外,"Java Present and Future"、"The Decorator Design Pattern in Depth"、"Fix This" 等文章深入探讨了Java的不同方面,如装饰者设计模式的深入分析,以及提供给中高级测试问题的"Fix This"部分。 整体上,...
Content-Centric Networking (CCN) has recently emerged as a clean-slate Future Internet architecture which has a completely different communication pattern compared with exiting IP network. Since the ...
Additionally, considering the potential need for system maintenance, the B/S (Browser/Server) design pattern is utilized, allowing server-side updates without requiring any changes on the client-side...
在并发编程中,保护性暂停模式(Guarded Suspension Design Pattern)是一种常用的同步机制,用于线程间的协作。该模式允许一个线程等待另一个线程的特定操作完成,然后继续执行。在该模式中,线程间共享的某个对象...
5 Design Methods for Cognitive Systems 6 Cognitive Systems 7 The Future of Cognitive Systems 8 Final Remarks Privacy-Preserving Record Linkage for Big Data: Current Approaches and Research Challenges...
- **Task and Future Types**: Task and future types are used extensively in PPL to represent ongoing computations. The article covers their usage and benefits over traditional threading mechanisms. -...
8. **Scalability and Performance**: The design considers future expansion and high traffic scenarios, ensuring the system can handle increased load without compromising performance. 9. **Error ...