http://www.cnblogs.com/zhiranok/archive/2012/07/30/fflib_framework.html
http://blog.chinaunix.net/uid-23093301-id-190969.html
https://msdn.microsoft.com/zh-cn/library/dd764564.aspx#Y300
What:
什么是future:future的原理是当你申请资源(计算资源或I/O资源)时,立即返回一个虚拟的资源句柄,当真正使用的时候,再将虚拟的句柄转化成真正的资源,相当于预获取。
How:
Future使用方法伪代码如下:
Future::Future(Job_func):
Thread.run(Job_func);
end
Future::get_result():
While(result == NULL):
Thread.sleep()
Return result
End
Why:
Future模式只有在并行运算的框架内才有意义。当一个逻辑操作设计的耗时操作比较多时,可以将耗时操作拆分成多个不太耗时的子操作,使子操作并行的执行,逻辑层依次获取子操作的结果。假设我们要执行一个逻辑操作,要求执行一次mysql查询,还要读一次文件,如果使用普通的同步方式:
Do:
query = Mysql_query()
file = File_read()
Do_thing(query, file)
Done
使用future模式示例如下:
Do:
Future a(Mysql_query)//! 非阻塞
Future b(File_read) //! 非阻塞
Query = a.get_result() //! 阻塞获取结果
File = b.get_result() //! 阻塞获取结果
Do_thing(query, file)
Done
这样sql查询和读取文件实现了并行运行,同步等待的时间为二者开销较大的运行时间。
When:
适于使用future模式的时机:在客户端,我们常常需要阻塞的获取结果,通过future模式可以大大提高响应速度。而在服务端程序,阻塞操作会降低系统的吞吐量,future模式试用的范围较窄,一般服务端采用异步回调的方式,将耗时的操作并行化,再通过回调方式将结果合并。Future构造时生成了虚拟的结果,如果使用这个结果越晚,当get_result时越不容易阻塞,所以从生成future到获取结果的间隔越长,future模式的功效越大。
================================
http://ymbian.blog.51cto.com/725073/147086
1引论

2 思想
一個簡單的Java程式片段示範可能像是這樣:
....
public Future request() {
final Future future = new Future();
new Thread() {
public void run() {
// 下面這個動作可能是耗時的
RealSubject subject = new RealSubject();
future.setRealSubject(subject);
}
}.start();
return future;
}
可是我要怎么判断我要的数据已经准备好?
通过自定义一个结果类,负责结果持有。
private String result;
private boolean isFinish = false;
public String getResult(){
return result;
}
public synchronized void setResult (String result){
this.result = result;
this.isFinish = true;
}
public synchronized boolean isFinish(){
return isFinish;
}
}
3 Just do it
- 希望能看到这个对象可用,并完成一些相关的后续流程。如果实在不可用,也可以进入其它分支流程。 //非阻塞
- “没有你我的人生就会失去意义,所以就算海枯石烂,我也要等到你。”(当然,如果实在没有毅力枯等下去,设一个超时也是可以理解的)//阻塞

总之,FutureTask封装了对realObject(Callable实例)的异步和同步操作,持有Callable实例,线程执行FutureTask对象。
下面的例子模拟的是一个会计算账的过程,主线程中已经获得其他帐户的总额了,为了不让主线程等待PrivateAccount 返回而启用新的线程去处理,并使用 FutureTask 对象来监控,最后需要计算总额的时候再尝试去获得PrivateAccount 的信息。
-
SumAccountExample.java
package com.gc.pattern;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class SumAccountExample {
public static void main(String[] args) {
// Init callable object and future task
Callable pAccount = new PrivateAccount();
FutureTask futureTask = new FutureTask(pAccount);
// Create a new thread to do so
Thread pAccountThread = new Thread(futureTask);
pAccountThread.start();
// Do something else in the main thread
System.out.println("Doing something else here.");
// Get the total money from other accounts
int totalMoney = new Random().nextInt(100000);
System.out.println("You have "+totalMoney+" in your other Accounts.");
System.out.println("Waiting for data from Private Account");
// If the Future task is not finished, we will wait for it
while (!futureTask.isDone()){
try{
Thread.sleep(5);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
Integer privataAccountMoney = null ;
// Since the future task is done, get the object back
try{
privataAccountMoney = (Integer)futureTask.get();
}
catch(InterruptedException e){
e.printStackTrace();
}
catch(ExecutionException e){
e.printStackTrace();
}
System.out.println("The total moeny you have is "+(totalMoney+privataAccountMoney.intValue()));
}
}
-
PrivateAccount.java
package com.gc.pattern;
import java.util.Random;
import java.util.concurrent.Callable;
class PrivateAccount implements Callable{
Integer totalMoney;
public Integer call() throws Exception {
// Simulates a time conusimg task, sleep for 10s
//与Callable 不同,它有返回值
Thread.sleep( 10000 );
totalMoney = new Integer(new Random().nextInt(10000));
System.out.println("You have "+totalMoney+" in your private Account. " );
return totalMoney;
}
}