java并发包:future模式

来源:互联网 发布:国语电影排行榜 知乎 编辑:程序博客网 时间:2024/05/20 06:23

本文转载至:http://blog.csdn.net/a910626/article/details/51900972

future模式是多线程开发中非常常见的一种设计模式,它的核心思想是异步调用。当我们需要调用一个函数方法时,如果这个函数执行很慢,那么我们就要进行等待。但有时候我们可能并不着急着要结果。因此,我们可以让被调用者立即返回,让他在后台慢慢处理这个请求。对于调用者来说,则可以先处理一些其他任务,在真正需要数据的场合再去尝试获得需要的数据。

  Future模式有点类似于商品订单。在网上购物时,提交订单后,在收货的这段时间里无需一直在家里等候,可以先干别的事情。类推到程序设计中时,当提交请求时,期望得到答复时,如果这个答复可能很慢。传统的时一直等待到这个答复收到时再去做别的事情,但如果利用Future设计模式就无需等待答复的到来,在等待答复的过程中可以干其他事情。

  对于future模式来说,虽然它无法立即给你需要的数据。但是,它会返回给你一个契约,将来,你可以凭借这个契约去重新获取你需要的信息。

Future模式java实现

(1) Data.Java

public interface Data {    public String getResult();}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

(2) RealData.java

public class RealData implements Data {    protected final String result;    public RealData(String para) {        //RealData的构造可能很慢,需要用户等待很久        StringBuffer sb=new StringBuffer();        for (int i = 0; i < 10; i++) {            sb.append(para);            try {                Thread.sleep(100);            } catch (InterruptedException e) {            }        }        result=sb.toString();    }    public String getResult() {        return result;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(3) FutureData.java

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();    }    public synchronized String getResult() {        while (!isReady) {            try {                wait();            } catch (InterruptedException e) {            }        }        return realdata.result;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

(4) Client.java

public class Client {    public Data request(final String queryStr) {        final FutureData future = new FutureData();        // RealData的构建很慢        new Thread() {                                                  public void run() {                                             RealData realdata = new RealData(queryStr);                future.setRealData(realdata);            }                                                       }.start();        return future;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

(5) Main.java

public class Main {    public static void main(String[] args) {        Client client = new Client();        Data data = client.request("a");        System.out.println("请求完毕");        try {            //这里可以用一个sleep代替了对其它业务逻辑的处理            Thread.sleep(2000);        } catch (InterruptedException e) {        }            //使用真实的数据        System.out.println("数据 = " + data.getResult());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果: 
请求完毕 
数据 = aaaaaaaaaa

future jdk实现

(1) RealData.java

import java.util.concurrent.Callable;public class RealData implements Callable<String> {    private String para;    public RealData(String para){        this.para=para;    }    @Override    public String call() throws Exception {        StringBuffer sb=new StringBuffer();        for (int i = 0; i < 10; i++) {            sb.append(para);            try {                Thread.sleep(100);            } catch (InterruptedException e) {            }        }        return sb.toString();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

(2) Main.java

import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.FutureTask;public class Main {    public static void main(String[] args) throws InterruptedException, ExecutionException {        //构造FutureTask        FutureTask<String> future = new FutureTask<String>(new RealData("a"));        ExecutorService executor = Executors.newFixedThreadPool(1);        //执行FutureTask,相当于上例中的 client.request("a") 发送请求        //在这里开启线程进行RealData的call()执行        executor.submit(future);        System.out.println("请求完毕");        try {        //这里依然可以做额外的数据操作,这里使用sleep代替其他业务逻辑的处理            Thread.sleep(2000);        } catch (InterruptedException e) {        }        //相当于上例中得data.getContent(),取得call()方法的返回值        //如果此时call()方法没有执行完成,则依然会等待        System.out.println("数据 = " + future.get());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

输出结果: 
请求完毕 
数据 = aaaaaaaaaa

0 0
原创粉丝点击