栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java多线程分批调用接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java多线程分批调用接口

线程池工具类

public class ThreadPoolUtil {

    public static final long DEFAULT_WAIT_SECONDS = 5000;

    private static class ThreadPool {
        private static final ThreadFactory namedThreadFactory =
            new ThreadFactoryBuilder().setNameFormat("thread-pool-%d").build();
        private static final ThreadPoolExecutor pool = new ThreadPoolExecutor(20, 40, 60L, TimeUnit.SECONDS,
            new LinkedBlockingQueue(200), namedThreadFactory, new ThreadPoolExecutor.DiscardPolicy()) {
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                super.afterExecute(r, t);
                printException(r, t);
            }
        };
    }

    
    public static ExecutorService getThreadPool() {
        log.info("ThreadPool task num now: {}", ThreadPool.pool.getActiveCount());
        return ThreadPool.pool;
    }

    private static void printException(Runnable r, Throwable t) {
        if (t == null && r instanceof Future) {
            try {
                Future future = (Future)r;
                if (future.isDone()) {
                    future.get();
                }
            } catch (CancellationException ce) {
                t = ce;
            } catch (ExecutionException ee) {
                t = ee.getCause();
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt(); // ignore/reset
            }
        }
        if (t != null) {
            log.error(t.getMessage(), t);
        }
    }

    
    public static  List executeTasks(List> callables, long waitSeconds) {
        return doExecute(callables, waitSeconds, ThreadPool.pool);
    }

    private static  List doExecute(List> callables, long waitSeconds, ThreadPoolExecutor executor) {
        try {
            List result = Lists.newArrayList();
            if (callables == null || callables.isEmpty()) {
                return result;
            }
            List> futures = executor.invokeAll(callables);
            try {
                for (Future future : futures) {
                    result.add(future.get(waitSeconds, TimeUnit.SECONDS));
                }
            } catch (Exception e) {
                throw e;
            } finally {
                for (Future future : futures) {
                    try {
                        future.cancel(true);
                    } catch (Exception e2) {
                    }
                }
            }
            return result;
        } catch (Exception e) {
            log.error("doExecute error", e);
            throw new RuntimeException(e.getMessage());
        }
    }
}

使用Callable

public String getTableOwnersByTableGuid(List tableGuids) {
        String ret = "[";
        List> callables = Lists.newArrayList();
        tableGuids.stream().forEach(tableGuid -> {
            Callable callable = () -> Optional.ofNullable(odpsTableManager.queryOdpsTableBaseInfo(tableGuid)).map(
                GetMetaTableBasicInfoResponse.Data::getOwnerId).orElse(null);
            callables.add(callable);
        });
        List ownerIdList = ThreadPoolUtil.executeTasks(callables, ThreadPoolUtil.DEFAULT_WAIT_SECONDS).stream().filter(
            Objects::nonNull).collect(Collectors.toList());
        for (String ownerId : ownerIdList) {
            ret += "'" + ownerId + "',";
        }
        return StringUtils.stripEnd(ret, ",") + "]";
    }

使用CompletableFuture

public String getTableOwnersByTableGuid1(List tableGuids) {
        String ret = "[";
        List> completableFutureList = Lists.newArrayList();
        tableGuids.stream().forEach(tableGuid -> {
            try {
                CompletableFuture future = CompletableFuture.supplyAsync(() -> odpsTableManager.queryOdpsTableBaseInfo(tableGuid).getOwnerId());
                completableFutureList.add(future);
            } catch (Exception e) {
                log.info("getTableOwnersByWorkOrderId queryOdpsTableBaseInfo fail");
            }
        });
        List ownerIdList = completableFutureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
        for (String ownerId : ownerIdList) {
            ret += "'" + ownerId + "',";
        }
        return StringUtils.stripEnd(ret, ",") + "]";
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号