zoukankan      html  css  js  c++  java
  • springboot 使用线程池处理多线程任务

    一、创建线程池

    @Configuration
    @EnableAsync
    public class SpringAsyncConfig {

    @Bean("taskExecutor")
    public Executor asyncServiceExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 设置核心线程数
    executor.setCorePoolSize(5);
    // 设置最大线程数
    executor.setMaxPoolSize(20);
    //配置队列大小
    executor.setQueueCapacity(Integer.MAX_VALUE);
    // 设置线程活跃时间(秒)
    executor.setKeepAliveSeconds(60);
    // 设置默认线程名称
    executor.setThreadNamePrefix("xcc");
    // 等待所有任务结束后再关闭线程池
    executor.setWaitForTasksToCompleteOnShutdown(true);
    //执行初始化
    executor.initialize();
    return executor;
    }
    }

    二、创建使用线程池的server(网上有人说线程任务最好放到一个server里面不然会造成循环依赖。。。未验证)
    public interface ThreadUtilsService {
    /**
    * 获取用户下的考勤记录
    * @param wechatUser
    */
    void setAttendance(WechatUser wechatUser, CountDownLatch countDownLatch);
    }


    @Service
    public class ThreadUtilsServiceImpl implements ThreadUtilsService {
    @Autowired
    private AttendanceService attendanceService;

    @Async("taskExecutor")
    @Override
    public void setAttendance(WechatUser wechatUser, CountDownLatch countDownLatch) {
    List<Attendance> currentClockListByOpenId = attendanceService.getCurrentClockListByOpenId(wechatUser);
    countDownLatch.countDown();
    wechatUser.setAttendanceList(currentClockListByOpenId);
    }
    }


    三、调用
    threadUtilsService.setAttendance(record,countDownLatch);



  • 相关阅读:
    表、索引元信息的更新
    接口
    SAP ALV OO 选择行打印
    SAP SMARTFORM 变量显示技巧
    SAP SMARTFORMS-基于内存方式在report程序中传递数据到smartforms显示
    PL/Proxy介绍
    pgpool介绍和安装经验
    pgbouncer介绍
    Hibernate使用时需要注意的几个小问题
    JDBC连接MySQL
  • 原文地址:https://www.cnblogs.com/cw828/p/14926751.html
Copyright © 2011-2022 走看看