JDK1.8中异步线程操作类CompletableFuture,可以更爽的异步编程


public class UserInfo {

    private String  userId;
    
    private String userName;
    
    private String nickName;

    /**
     * Getter method for property <tt>userId</tt>.
     * 
     * @return property value of userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * Setter method for property <tt>userId</tt>.
     * 
     * @param userId value to be assigned to property userId
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * Getter method for property <tt>userName</tt>.
     * 
     * @return property value of userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Setter method for property <tt>userName</tt>.
     * 
     * @param userName value to be assigned to property userName
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * Getter method for property <tt>nickName</tt>.
     * 
     * @return property value of nickName
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * Setter method for property <tt>nickName</tt>.
     * 
     * @param nickName value to be assigned to property nickName
     */
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    
    
    
    
    
}

public class UserService {

    public UserInfo queryUserInfoById(String userId){
        UserInfo userInfo=new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setUserName(userId+"yy");
        userInfo.setNickName(userId+"nickName");
        System.out.println("--"+userInfo);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
        }
        return userInfo;
    }
}
public class CompletableFutureTest {

   
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        
        Executor executor = Executors.newFixedThreadPool(100);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<String> userIds=new ArrayList<String>();
        userIds.add("123");
        userIds.add("124");
        userIds.add("125");
        userIds.add("126");
        List<CompletableFuture<UserInfo>>  userInfoList=userIds.stream().map((s)->{
            return CompletableFuture.supplyAsync(()->{
                UserService userService=new UserService();
                return userService.queryUserInfoById(s);
            },executor);
        }).collect(Collectors.toList());
        
        CompletableFuture<Void> allFutures=CompletableFuture.allOf(userInfoList.toArray(new CompletableFuture[userInfoList.size()]));
        
        CompletableFuture<List<UserInfo>> allFutureList=allFutures.thenApply(v->{
            return userInfoList.stream().map(a -> {
                try {
                    //return a.get(10,TimeUnit.MILLISECONDS);
                    return a.join();
                } catch (Exception e) {
                }
                return null;
            }).collect(Collectors.toList()); 
        });
        List<UserInfo> list = allFutureList.join();
        for(UserInfo u:list){
            System.out.println(u.getNickName()+"--"+u.getUserId()+"--"+u.getUserName());
        }
        stopWatch.stop();
        long t = stopWatch.getTotalTimeMillis();
        System.out.println(t);
       
    }

}

猜你喜欢

转载自blog.csdn.net/u011955252/article/details/81190854
今日推荐