下面小编就为大家带来一篇老生常谈java中的future模式。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
jdk1.7.0_79
本文实际上是对上文《简单谈谈threadpoolexecutor线程池之submit方法》的一个延续或者一个补充。在上文中提到的submit方法里出现了futuretask,这不得不停止脚步将方向转向java的future模式。
future是并发编程中的一种设计模式,对于多线程来说,线程a需要等待线程b的结果,它没必要一直等待b,可以先拿到一个未来的future,等b有了结果后再取真实的结果。
executorservice executor = executors.newsinglethreadexecutor();
future<string> future = executor.submit(callable); //主线程需要callable线程的结果,先拿到一个未来的future
system.out.println(future.get()); //有了结果后再根据get方法取真实的结果,当然如果此时callable线程如果没有执行完get方法会阻塞执行完,如果执行完则直接返回结果或抛出异常
也就是说,future它代表一个异步计算的结果。
上面就代表了future模式的执行原理,根据网上的例子,我们可以来自己实现一个future模式。
package com.future;
/**
* 数据结果
* created by yulinfeng on 6/18/17.
*/
public interface data {
string getresult() throws interruptedexception;
}
package com.future;
/**
* 结果的真实计算过程
* created by yulinfeng on 6/18/17.
*/
public class realdata implements data {
protected string data;
public realdata(string data) {
try {
system.out.println("正在计算结果");
thread.sleep(3000); //模拟计算
} catch (interruptedexception e) {
e.printstacktrace();
}
this.data = data + “ world”;
}
public string getresult() throws interruptedexception {
return data;
}
}
package com.future;
/**
* 真实结果realdata的代理
* created by yulinfeng on 6/18/17.
*/
public class futuredata implements data {
realdata realdata = null; //对realdata的封装,代理了realdata
boolean isready = false; //真实结果是否已经准备好
public synchronized void setresultdata(realdata realdata) {
if (isready) {
return;
}
this.realdata = realdata;
isready = true;
notifyall(); //realdata已经被注入到了futuredata中,通知getresult方法
}
public synchronized string getresult() throws interruptedexception {
if (!isready) {
wait(); //数据还未计算好,阻塞等待
}
return realdata.getresult();
}
}
package com.future;
/**
* client主要完成的功能包括:1. 返回一个futuredata;2.开启一个线程用于构造realdata
* created by yulinfeng on 6/18/17.
*/
public class client {
public data request(final string string) {
final futuredata futuredata = new futuredata();
/*计算过程比较慢,单独放到一个线程中去*/
new thread(new runnable() {
public void run() {
realdata realdata = new realdata(string);
futuredata.setresultdata(realdata);
}
}).start();
return futuredata; //先返回一个“假”的futuredata
}
}
/**
* 负责调用client发起请求,并使用返回的数据。
* created by yulinfeng on 6/18/17.
*/
public class main {
public static void main(string[] args) throws interruptedexception {
client client = new client();
system.out.println("准备计算结果");
data data = client.request("hello"); //立即返回一个“假”的futuredata,可以不用阻塞的等待数据返回,转而执行其它任务
system.out.println("执行其它任务");
thread.sleep(3000); //模拟执行其它任务
system.out.println("数据的计算结果为:" + data.getresult());
}
}
仔细阅读以上程序对future模式的实现不难发现,future模式是异步请求和代理模式的结合。当然在jdk中已经为我们实现好了future模式。
修改realdata类:
package com.future;
import java.util.concurrent.callable;
/**
* 结果的真实计算过程
* created by yulinfeng on 6/18/17.
*/
public class realdata2 implements callable<string> {
protected string data;
public realdata2(string data) {
this.data = data;
}
public string call() throws exception {
try {
system.out.println("正在计算结果");
thread.sleep(2000); //模拟计算结果
} catch (interruptedexception e) {
e.printstacktrace();
}
this.data = data + " world";
return data;
}
}
修改main测试类:
package com.future;
import java.util.concurrent.executionexception;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
import java.util.concurrent.future;
/**
* 负责调用executor的submit,并使用返回的数据。
* created by yulinfeng on 6/18/17.
*/
public class main2 {
public static void main(string[] args) throws interruptedexception, executionexception {
executorservice client = executors.newsinglethreadexecutor(); //类似client
system.out.println("准备计算结果");
future<string> data = client.submit(new realdata2("hello")); //类似client.request
system.out.println("执行其它任务");
thread.sleep(3000);
system.out.println("数据的计算结果为:" + data.get());
}
}
现在回到上文还未解决完的abstractexecutorservice#submit方法。
类比上面的client#request方法,在client#request中先创建一个futuredata实例,而在abstractexecutorservice#submit中则是创建一个futuretask实例,接着client#request新创建一个线程用于异步执行任务,并直接返回futuredata,而在abstractexecutorservice#submit中同样也将任务交给了execute方法,并直接返回futuretask。当然jdk中future模式的实现更为复杂。
在《threadpoolexecutor线程池原理及其execute方法》中我们讲解了execute方法,在threadpoolexecutor$worker#runworker方法第1145行中是对task任务的调用:
//threadpoolexecutor$worker#runworker
task.run();
submit调用execute以执行run方法,实际执行的是futuretask中的run方法。在futuretask#run中,可以看到对任务callable类型的task异步的执行,以及结果的保存。
以上就是java中的future模式详解的详细内容。