/**
* 数据接口
*/
public interface Data {
public String getResult();
}
/**
* 最终需要使用的数据模型
*/
public class RealData implements Data{
protected final String result;
public RealData(String result) { //模型大的数据,构造比较慢用户等待很久
StringBuffer sb = new StringBuffer();
sb.append(result);
for (int i = 0; i < 20; i++) {
sb.append(i);
sb.append(" ");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.result = sb.toString();
}
@Override
public String getResult() {
return result;
}
}
/**
* 真实数据的代理,封装真实数据的等待过程
*/
public class FutureData implements Data {
protected RealData realData = null;
protected boolean isReady = false;
public synchronized void setRealData(RealData realData){
if (isReady){
return;
}
this.realData = realData;
isReady = true;
notifyAll();//RealData已经被注入,通知getResult()
}
@Override
public synchronized String getResult() { //等待RealData被注入
while (!isReady){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return realData.result;
}
}
public class Client {
public Data request(final String queryStr){
final FutureData future = new FutureData();
new Thread(){
@Override
public void run() { //RealData的构造很慢,所以在单独的线程中进行
RealData realData = new RealData(queryStr);
future.setRealData(realData);
}
}.start();
return future; //立即返回
}
}
/**
* Future模式的简单实现
* 参与者
* Main:系统启动,调用Client发出请求
* Client:返回Data对象,立即返回FutureData,并开启线程装配RealData
* Data:返回数据的接口
* FutureData:封装真实数据,需要装配RealData
* RealData:真实数据
*/
public class Main {
public static void main(String[] args){
Client client = new Client();
Data data = client.request("name"); //立即返回的FutureData
System.out.println("请求完毕");
try {
Thread.sleep(2000); //模拟其他业务,这段时间RealData被创建,充分利用时间
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("data:"+data.getResult());//真实数据
}
//请求完毕
//data:name0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
}
jdk自带
- boolean cancel(boolean mayInterruptIfRunning) //取消任务
- boolean isCancelled() //是否已经取消
- boolean isDone() //是否已完成
- V get() //取得返回对象
- V get(long timeout, TimeUnit unit) //取得返回对象,可以设置超时时间
import java.util.concurrent.Callable;
/**
* 使用JDK内置的Future
*/
public class RealDataJDK implements Callable<String> {
private String result;
public RealDataJDK(String result) {
this.result = result;
}
@Override
public String call() throws Exception {
//执行很慢的业务逻辑
StringBuffer sb = new StringBuffer();
sb.append(result);
for (int i = 0; i < 10; i++) {
sb.append(i);
sb.append(" ");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class MainJDK {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> future= new FutureTask<>(new RealDataJDK("name"));
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(future);
System.out.println("请求完毕");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//取得call()方法的返回值,如果此时call()方法没有执行完成,则依然会等待
System.out.println("data:"+future.get());
System.out.println("执行完成");
executor.shutdown();
}
}