Java 异步编程

时间:2019-08-18
本文章向大家介绍Java 异步编程,主要包括Java 异步编程使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
public List<Long> syncHandleList(List<UgcDpSync> ugcDpSyncList){
        if (CollectionUtils.isEmpty(ugcDpSyncList)){
            return Collections.emptyList();
        }
        CompletionService<List<Long>> cs = new ExecutorCompletionService<>(executor);
        List<Long> list = new ArrayList<>();
        int loop = 0;
        try {
            cs.submit(() -> {
                List<Long> tmpList = new ArrayList<>();
                for (UgcDpSync ugcDpSync : ugcDpSyncList) {
                    if (ugcDpSync.getCommentId() != null){
                        System.out.println(ugcDpSync);
                        tmpList.add(ugcDpSync.getCommentId());
                    }
                }
                return tmpList;
            });
            loop++;
            if (loop % 1 == 0) {
                for (int i = 0; i < 1; i++) {
                    try {
                        List<Long> result = cs.take().get();
                        if (CollectionUtils.isNotEmpty(result)) {
                            list.addAll(result);
                        }
                    } catch (Exception e) {
                    }
                }
            }
        } catch (Exception e) {
        }
        try {
            Thread.sleep(500);
        } catch (Exception e1) {
        }
        return list;
    }

原文地址:https://www.cnblogs.com/cjn123/p/11373149.html