java初入多线程18

Future 模式

  • 该模式核心思想是异步调用。
Future 异步调用模式
public class Main {
    public static void main(String[] args) {
        Client client  = new Client() ;
        
        Data data = client.request("name");
        System.out.println("请求完毕");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
        }
        
        System.out.println("数据  = " + data.getResult());
    }
}
public class Client {
    public Data request(final String queryStr){
        final FutureData future = new FutureData() ;
        new Thread(){
            public void run() {
                RealData realData = new RealData(queryStr);
                future.setRealData(realData);
            }   ;
        }.start();  
        return future ;
    }   
}
public class RealData implements Data {
    protected  final String result ;
    public RealData(String result) {
        super();
        StringBuffer buffer =new StringBuffer();
        for(int i = 0 ; i < 10 ; i++){
            buffer.append(result+" ");
            try {
                Thread.sleep(100);
            } catch (Exception e) {     
            }
        }   
        this.result = buffer.toString();
    }
    @Override
    public String getResult() {
        return result;
    }
}
public interface Data {
    public String getResult();
}
public class FutureData implements Data {
    protected boolean isReady = false ;
    protected RealData  realData = null ;
    public synchronized  void setRealData(RealData realData) {
        if(isReady){
            return ;
        }
        this.realData = realData;
        isReady = true ;
        notifyAll();
    }
    @Override
    public synchronized String getResult() {
        while(!isReady){
            try {
                wait();
            } catch (Exception e) {
            }
        }
        return realData.result; 
    }
}

jdk 中的Future 模式

内置实现Future
public class FurureMain {

    public static void main(String[] args)  throws Exception{
        //构造任务
        FutureTask<String> future = new FutureTask<String>( new RealDataCall("a"));
        ExecutorService executor =Executors.newFixedThreadPool(1);
        executor.submit(future);
        System.out.println("请求完毕");
        try {   
            Thread.sleep(2000);
        } catch (Exception e) {
            // TODO: handle exception
        }   
        System.out.println("数据= : "+ future.get());
    }   
}
public class RealDataCall implements Callable<String> {
    private String para ;
    public RealDataCall(String para) {
        super();
        this.para = para;
    }
    @Override
    public String call() throws Exception {
        StringBuffer buffer =new StringBuffer();
        for(int i = 0 ; i < 10 ; i++){
            buffer.append(para+" ");
            try {
                Thread.sleep(100);
            } catch (Exception e) {         
            }
        }
        return buffer.toString();
    }
}
  1. 在这里面我们首先创建FutureTask对象实例, 表示该任务是有返回值的。
  2. 第二步 使用Callable 产生数据。
  3. 提交任务到线程池。 作为任务的简单提交。用get把结果查出来
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 下面是我自己收集整理的Java线程相关的面试题,可以用它来好好准备面试。 参考文档:-《Java核心技术 卷一》-...
    阿呆变Geek阅读 14,983评论 14 507
  • 一.线程安全性 线程安全是建立在对于对象状态访问操作进行管理,特别是对共享的与可变的状态的访问 解释下上面的话: ...
    黄大大吃不胖阅读 893评论 0 3
  • Java-Review-Note——4.多线程 标签: JavaStudy PS:本来是分开三篇的,后来想想还是整...
    coder_pig阅读 1,705评论 2 17
  • MongoDB的优势: 性能快 查询语言丰富 高可用 横向扩展 和关系型数据库的比较 使用一个database u...
    yangweigbh阅读 547评论 0 51
  • 东汉末年,天下大乱。各路英雄招兵买马,都准备自主创业,大干一场。这其中不乏很有头脑的优秀领导人,比如曹操。当年他的...
    小十八阅读 4,379评论 3 3