• completableFuture


    completableFuture jian简化java异步开发

    package org.coffee.mala.test;
    
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutionException;
    import java.util.function.Supplier;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class BookAsyncService {
    
    	public BookDetails getBookDetails(String bookId) throws InterruptedException, ExecutionException {
    
    		Book book = getBook(bookId);
    		CompletableFuture<ChuBanShe> chuBanShe = CompletableFuture.supplyAsync(new Supplier<ChuBanShe>() {
    
    			@Override
    			public ChuBanShe get() {
    
    				return getChuBanShe(book.getChuBanSheId());
    			}
    
    		});
    		CompletableFuture<GongYingShang> gongYingShang = CompletableFuture.supplyAsync(new Supplier<GongYingShang>() {
    
    			@Override
    			public GongYingShang get() {
    
    				return getGongYingShang(book.getGongYingShangId());
    			}
    
    		});
    		CompletableFuture<BookDetails> result = chuBanShe.thenCombine(gongYingShang, (chs, gys) -> {
    			BookDetails bookDetails = new BookDetails();
    			bookDetails.setBook(book);
    			bookDetails.setChuBanShe(chs);
    			bookDetails.setGongYingShang(gys);
    			return bookDetails;
    		});
    		return result.get();
    	}
    
    	public Book getBook(String bookId) {
    
    		try {
    			Thread.sleep(2000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		Book book = new Book();
    		book.setId(bookId);
    		book.setName("shu ming");
    		book.setChuBanSheId("chuBanSheId");
    		book.setGongYingShangId("gongYingShangId");
    		return book;
    	}
    
    	public ChuBanShe getChuBanShe(String chuBanSheId) {
    
    		try {
    			Thread.sleep(2000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		ChuBanShe chuBanShe = new ChuBanShe();
    		chuBanShe.setId(chuBanSheId);
    		chuBanShe.setName("chu ban she");
    		return chuBanShe;
    	}
    
    	public GongYingShang getGongYingShang(String gongYingShangId) {
    
    		try {
    			Thread.sleep(2000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		GongYingShang gongYingShang = new GongYingShang();
    		gongYingShang.setId(gongYingShangId);
    		gongYingShang.setName("gong ying shang");
    		return gongYingShang;
    	}
    
    }
    
  • 相关阅读:
    谷歌火狐浏览器限制的端口
    Web Api 内部数据思考 和 利用http缓存优化 Api
    Web Api 端点设计 与 Oauth
    FI-FBV0
    Others-SAP hybris 介绍
    ABAP-表修改日志存储
    TR-银行通信相关文档
    ABAP-Eclipse ADT中创建ABAP CDS视图
    TR-业务流程图
    FPM-OVP增强实例-银行账户
  • 原文地址:https://www.cnblogs.com/zhucww/p/11196272.html
Copyright © 2020-2023  润新知