Future模式多线程并发处理

本文介绍了一个用于异步多线程批量校验任务的Java实现方案。该方案通过使用线程池来并行处理多个校验任务,提高了校验效率,并在出现超时或异常时能够及时终止任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.taobao.pagani.huntingnew.biz.benefit.check;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.taobao.eagleeye.EagleEye;
import com.taobao.pagani.huntingnew.common.threadpool.ThreadPoolExecutorFactory;
import com.taobao.pagani.huntingnew.common.util.logger.LoggerUtil;
import com.taobao.pagani.huntingnew.pipeline.spi.PipelineContext;
import com.taobao.pagani.huntingnew.switchs.PaganiSwitchConfig;
import com.taobao.uic.common.domain.BaseUserDO;
import jodd.exception.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

/**
 *
 * @author zhenyuan.he
 * @version $Id: InfactDataBatchEngine.java, v 0.1 2018年8月29日 下午8:44:11 zhenyuan.he Exp $
 */
public class BatchCheckEngine {

    protected static final Logger logger = LoggerFactory.getLogger(BatchCheckEngine.class);

    /**
     * 异步多线程调用
     * @param benefitChecks
     * @param baseUserDO
     * @param sceneId
     * @param extraParam
     * @return
     */
    public static boolean asynExecuteTask(Map<String, BenefitCheck> benefitChecks,
                                          BaseUserDO baseUserDO, long sceneId,
                                          Map<String, Object> extraParam) {

        Future<Boolean> excuteFuture = null;
        try {

            if (CollectionUtils.isEmpty(benefitChecks)) {
                LoggerUtil.info(logger, "parallelExecuteTask is null,benefitChecks is null");
                return true;
            }

            ThreadPoolExecutor threadPoolExecutor = ThreadPoolExecutorFactory.getInstance();

            List<Future<Boolean>> futures = new ArrayList<>();

            for (Entry<String, BenefitCheck> benefitCheck : benefitChecks.entrySet()) {

                CheckTask checkTask = new CheckTask(benefitCheck.getValue(), baseUserDO, sceneId,
                    extraParam, EagleEye.getRpcContext());

                Future<Boolean> future = threadPoolExecutor.submit(checkTask);

                if (future != null) {
                    futures.add(future);
                }

            }

            for (int index = 0; index < futures.size(); index++) {

                excuteFuture = futures.get(index);

                Boolean infactData = excuteFuture.get(PaganiSwitchConfig.CHECK_TASK_TIME_OUT,
                    TimeUnit.MILLISECONDS);

                if (infactData != null && !infactData) {
                    return false;
                }

            }

            return true;

        } catch (TimeoutException timeoutException) {
            // 超时异常不打日志
            PipelineContext.addDebugLog("校验超时,错误详情",
                ExceptionUtil.exceptionChainToString(timeoutException).substring(0, 200));
            return false;
        } catch (Throwable e) {

            if (excuteFuture != null) {
                excuteFuture.cancel(true);
            }

            LoggerUtil.error(logger, e, "parallelExecuteTask error,userId=", baseUserDO.getUserId(),
                "sceneId=", sceneId, "extraParam=", extraParam);

            return false;

        }
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值