Callable 多线程

 多线程 callable /future 实现伪代码:
        int index=1;
        int pagSize=1000;
        MagicMirrorPictureBaseInfoDao pictureBaseInfoDao=new MagicMirrorPictureBaseInfoDao();
        List<MagicMirrorPictureBaseInfo> pictureBaseInfoList=pictureBaseInfoDao.queryImageInfo(index, pagSize);
        List<Future<Boolean>> taskResultList = new ArrayList<>();
        while (pictureBaseInfoList !=null && !pictureBaseInfoList.isEmpty() && !ProductImageQrcodeWorker.shouldStrop()){
            for (MagicMirrorPictureBaseInfo baseInfo:pictureBaseInfoList){

                if(ProductImageQrcodeWorker.shouldStrop()){
                    break;
                }
                ImageQrcodeTask task=new ImageQrcodeTask(baseInfo);
                Future<Boolean> taskResult= BusinessTaskPool.getInstance().getThreadPoolTaskExecutor().submit(task);
                taskResultList.add(taskResult);
                if (taskResultList.size()>5){
                    checkResultList(taskResultList);
                    taskResultList=new ArrayList<>();
                }
            }
            checkResultList(taskResultList);
            index ++;
            pictureBaseInfoList=pictureBaseInfoDao.queryImageInfo(index, pagSize);

        }

 /**
 * 多线程check任务完成(返回值为boolean)
 * @param taskResultList
 */
public void checkResultList(List<Future<Boolean>> taskResultList) {
    if (!taskResultList.isEmpty()) {
        for (Future<Boolean> f : taskResultList) {
            try {
                f.get();
            } catch (Exception e) {

                clog.warn(CLOG_TITLE,"多线程任务失败");
            }
        }
    }
}

/**

  • 有返回值的
    */
    private List waitTaskComplete(List<Future> taskResultList){
    List list=new ArrayList<>();
    for(Future tr : taskResultList){
    try {
    list.add(tr.get());
    } catch (Exception e) {
    cLogger.warn(TITLE,e);
    }
    }
    return list;
    }

/**

  • 线程池

  • @date 2017/11/15
    */
    public final class BusinessTaskPool {
    private static final Integer POOL_SIZE = 20;
    private static final Integer MAX_POOL_SIZE = 100;
    private static final Integer QUEUE_CAPACITY = 100;
    private static final Integer KEEP_ALIVE_SECONDS = 5;
    private static BusinessTaskPool businessTaskPool = new BusinessTaskPool();
    private ThreadPoolTaskExecutor poolTaskExecutor;

    private BusinessTaskPool(){
    poolTaskExecutor = new ThreadPoolTaskExecutor();
    poolTaskExecutor.setCorePoolSize(POOL_SIZE);
    poolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
    poolTaskExecutor.setQueueCapacity(QUEUE_CAPACITY);
    poolTaskExecutor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
    poolTaskExecutor.initialize();
    }

    /**

    • 设置线程池数量
    • @param poolSize
      */
      public void setPoolSize(Integer poolSize){
      poolTaskExecutor.setCorePoolSize(poolSize);
      }

    public static BusinessTaskPool getInstance(){
    return businessTaskPool;
    }

    public ThreadPoolTaskExecutor getThreadPoolTaskExecutor(){
    return this.poolTaskExecutor;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值