多线程执行任务,返回执行结果

  • package com.express.otp.ffs.commonorginfo.utils;

    import net.ytoframework.kernel.core.consts.SysErrorConsts; import
    net.ytoframework.kernel.core.exception.YtoException; import
    org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

    import java.util.ArrayList; import java.util.List; import
    java.util.concurrent.*;

    /** * 并行执行器 *

    • */

public class ConcurrentExecutor {
private static ThreadPoolTaskExecutor threadPoolTaskExecutor;

   static {
       threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
       threadPoolTaskExecutor.setCorePoolSize(5);
       threadPoolTaskExecutor.setMaxPoolSize(10);
       threadPoolTaskExecutor.setQueueCapacity(50);
       threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
       threadPoolTaskExecutor.setThreadNamePrefix("YtoExecutor-");
       threadPoolTaskExecutor.initialize();
   }

   /**
    * 执行一组有返回值的任务
    * @param callableList 任务列表
    * @param timeout 任务超时时间,单位毫秒
    * @param <T>
    * @return
    */
   public static <T> List<T> execute(List<Callable<T>> callableList,
                                     long timeout) {
       if (callableList == null || callableList.isEmpty()) {
           return null;
       }
       List<T> resultList = new ArrayList<>();
       List<Future<T>> futureList = new ArrayList<>();
       for (Callable<T> callable : callableList) {
           Future<T> future = threadPoolTaskExecutor.submit(callable);
           futureList.add(future);
       }
       for (Future<T> future : futureList) {
           try {
               T result = future.get(timeout, TimeUnit.MILLISECONDS);
               if (result != null) {
                   resultList.add(result);
               }
           } catch (InterruptedException e) {
               e.printStackTrace();
               throw new YtoException(SysErrorConsts.SYS_ERROR_CODE, "多线程执行异常");
           } catch (ExecutionException e) {
               e.printStackTrace();
               throw new YtoException(SysErrorConsts.SYS_ERROR_CODE, "多线程执行异常");
           } catch (TimeoutException e) {
               e.printStackTrace();
               throw new YtoException(SysErrorConsts.SYS_ERROR_CODE, "多线程执行异常");
           }
       }
       return resultList;
   }

   /**
    * 执行一组没有返回值的任务
    *
    * @param runnableList 任务列表
    * @param timeout 任务超时时间,单位毫秒
    */
   public static void submit(List<Runnable> runnableList,
                             long timeout,
                             ThreadPoolTaskExecutor threadPoolTaskExecutor) {
       if (runnableList == null || runnableList.isEmpty()) {
           return;
       }
       List<Future<?>> futureList = new ArrayList<>();
       for (Runnable runnable : runnableList) {
           Future future = threadPoolTaskExecutor.submit(runnable);
       }
       for (Future<?> future : futureList) {
           try {
               future.get(timeout, TimeUnit.MILLISECONDS);
           } catch (TimeoutException e) {
               e.printStackTrace();
               throw new YtoException(SysErrorConsts.SYS_ERROR_CODE, "多线程执行异常");
           } catch (InterruptedException e) {
               e.printStackTrace();
               throw new YtoException(SysErrorConsts.SYS_ERROR_CODE, "多线程执行异常");
           } catch (ExecutionException e) {
               e.printStackTrace();
               throw new YtoException(SysErrorConsts.SYS_ERROR_CODE, "多线程执行异常");
           }
       }
   } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值