Executors 线程池 ➕ 计数器

可配置线程数量

@Component
public class DemoDataThreadPool {

    @Value("thread.config.count")
    private String configCount;

    private ExecutorService executorService;

    public ExecutorService getExecutorService(){
        if (executorService == null){
            //使用线程池进行任务
            int threadCount = Integer.valueOf(configCount);
            final long startTime = System.currentTimeMillis();
            if (executorService == null) {
                ThreadFactory threadFactory = new ThreadFactoryBuilder()
                        .setNameFormat("Demo-%d")
                        .setDaemon(true)
                        .build();
                final long middleTime = System.currentTimeMillis();
                executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
            }
            final long endTime = System.currentTimeMillis();
            return executorService;
        }
        return executorService;
    }
}

引用线程池:

@Service
public class DemoDataAction {

    @Autowired
    DemoDataThreadPool demoDataThreadPool;
    @Autowired
    DemoService demoService;

    public void syncDemoData(){
//        int pageSize = 1000;
//        List limitList = Lists.newArrayList();
//
//        Integer pageNum = (limitList.size() % pageSize == 0) ? limitList.size()/pageSize : limitList.size()/pageSize + 1;
//        for (int i = 0; i < pageNum; i++) {
//            List resultList = demoService.queryBatchTask();
//            if (resultList == null || resultList.size() < 0) {
//                return;
//            }
//            allList.addAll(resultList);
//        }

        List allList = demoService.queryBatchTask();
        CountDownLatch countDownLatch = new CountDownLatch(allList.size());
        List<Map<String, Object>> mapList = Lists.newArrayList();
        for (int i = 0; i < allList.size(); i++) {
            BatchTaskOperation taskOperation = new BatchTaskOperation(demoService, mapList, countDownLatch);
            demoDataThreadPool.getExecutorService().execute(taskOperation);
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            //log
        }
    }
}

执行线程任务

@Log4j
@Component
public class BatchTaskOperation implements Runnable{

    private DemoService demoService;
    private List<Map<String, Object>> mapList;
    private CountDownLatch countDownLatch;

    public  BatchTaskOperation(DemoService demoService, List<Map<String, Object>> mapList, CountDownLatch countDownLatch){
        this.demoService = demoService;
        this.mapList = mapList;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        this.operationHandler();
    }

    public void operationHandler(){
        try {
            final String threadName = Thread.currentThread().getName();
            //TODO
            System.out.println("threadName:" + threadName);
        }catch (Exception e){
            //log
        }finally {
            countDownLatch.countDown();
        }
    }
}

自定义线程池:


@Log4j
public class DemoTaskMain {

    private static Logger logger = LoggerFactory.getLogger(DemoTaskMain.class);
    private static String CLASS_NAME = "DemoTaskMain";

    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 8;
    private static ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("process-task-thread-%d").build();
    private static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(100);
    private static ExecutorService executorService = new ThreadPoolExecutor(
            CORE_POOL_SIZE,
            MAX_POOL_SIZE,
            60L,
            TimeUnit.SECONDS,
            workQueue,
            threadFactory,
            //调用者执行策略(CallerRunsPolicy:既不抛异常,也不丢任务,让主线程执行)
            new ThreadPoolExecutor.CallerRunsPolicy());

    public void handlerTaskMain(){
        logger.info(CLASS_NAME, "handlerTaskMain", "当前时间:"+DateUtiles.formatDate(new Date()));

        //清除工作队列中未完成的任务
        workQueue.clear();
        //所有任务类型
        String[] taskTypes = {"1","2","3"};

        //所有任务类型
        for (String taskType : taskTypes) {
            if (StringUtils.isEmpty(taskType)){
                continue;
            }
            logger.info(CLASS_NAME, "handlerTaskMain", "任务类型:"+taskType);

            HashMap<Object, Object> paramsMap = Maps.newHashMap();
            List<Map<String, Object>> batchTaskList = taskSerice.getDemoTaskBatch();
            batchTaskList = batchTaskList != null ? batchTaskList : Collections.EMPTY_LIST;
            logger.info(CLASS_NAME, "handlerTaskMain", "任务数据量:"+ batchTaskList.size());

            //主线程将各个批次的任务封装到Callable,然后提交到队列
            for (Map<String, Object> taskMap : batchTaskList) {
                executorService.submit(new DemoCallableTask(taskMap));
            }
        }

    }

    /**
     * 内部类的实现方式更加优雅:无需返回值
     * 如果执行中出现异常,会在该异步线程中打印错误日志
     */
    private class DemoCallableTask implements Callable<Void> {
        private Map<String, Object> batchTaskMap;

        public DemoCallableTask(Map<String, Object> map){
            this.batchTaskMap = map;
        }

        @Override
        public Void call() throws Exception {
            //获取参数
            String idBatch = (String) batchTaskMap.get("idBatch");
            String threadName = Thread.currentThread().getName();
            //线程标识
            String seqhread = UUID.randomUUID().toString().replaceAll("-", "");
            //TODO
            return null;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

time Friend

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值