Java线程池

创建线程池

package com.firefly.performance.core.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolCommon {

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 核心池大小
        taskExecutor.setCorePoolSize(5);
        // 最大线程数
        taskExecutor.setMaxPoolSize(20);
        // 队列程度
        taskExecutor.setQueueCapacity(100);
        // 线程空闲时间
        taskExecutor.setKeepAliveSeconds(60);
        // 线程前缀名称
        taskExecutor.setThreadNamePrefix("asyncExecutor--");
        // 该方法用来设置 线程池关闭 的时候 等待 所有任务都完成后,再继续 销毁 其他的 Bean,
        // 这样这些 异步任务 的 销毁 就会先于 数据库连接池对象 的销毁。
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        // 任务的等待时间 如果超过这个时间还没有销毁就 强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
        taskExecutor.setAwaitTerminationSeconds(60);
        // 线程不够用时由调用的线程处理该任务
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return taskExecutor;
    }

}

调用

package com.firefly.performance.core.service.personal.performance;

import com.firefly.performance.api.enums.basic.config.PerformanceTypeEnum;
import com.firefly.performance.core.config.EmailNoticeConfigEnum;
import com.firefly.performance.core.dao.basic.config.TimeControlDao;
import com.firefly.performance.core.dao.performance.PersonalPerformanceDao;
import com.firefly.performance.core.entity.basic.config.TimeControl;
import com.firefly.performance.core.entity.performance.PersonalPerformance;
import com.firefly.performance.core.service.PushAppMessageService;
import com.firefly.performance.core.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhaocn
 * @since 2022-10-13
 */
@Slf4j
@Service
public class AgreementConfigService {

    @Resource
    private PersonalPerformanceDao personalPerformanceDao;
    @Resource
    private PushAppMessageService pushAppMessageService;
    @Resource
    private ThreadPoolTaskExecutor taskExecutor;
    @Resource
    private TimeControlDao timeControlDao;

    

    public Boolean remindBatch(List<Long> performanceIds){
        //根据个人绩效id查询员工邮箱
        Assert.listNotEmpty(performanceIds,"请选择员工!");
        List<PersonalPerformance> personalPerformances = personalPerformanceDao.selectBatchIds(performanceIds);
        if (CollectionUtils.isNotEmpty(personalPerformances)) {
            Map<String, List<PersonalPerformance>> performanceListMap = personalPerformances.stream().collect(Collectors.groupingBy(data -> MessageFormat.format("{0}#{1}", data.getYear(), data.getCycle())));
            for (Map.Entry<String, List<PersonalPerformance>> entry : performanceListMap.entrySet()) {
                List<PersonalPerformance> tempList = entry.getValue();
                if (CollectionUtils.isNotEmpty(tempList)) {
                    PersonalPerformance performance = tempList.get(0);
                    TimeControl timeControl = timeControlDao.selectTimeControlByYearAndCycle(performance.getFirmId(), performance.getYear(), performance.getCycle(), PerformanceTypeEnum.PERSONAL.getType());
                    taskExecutor.execute(() -> pushAppMessageService.sendAppMessageToEmployee(Collections.singletonList(timeControl), EmailNoticeConfigEnum.SALARY_AGREEMENT, tempList));
                }
            }
        }
        return true;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值