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);



  • 相关阅读:
    Android 报错Android
    转:JavaWeb学习总结(一) 写得相当不错
    infer 编译代码审查命令记录
    转:infoQ 2015开发者资料下载
    转:java 进阶之路
    转:使用gradle 构建编译程序
    web开发者的博客
    转:http2基本中文翻译
    转:http2的资料与使用
    转:百度手机地图网络性能优化实践
  • 原文地址:https://www.cnblogs.com/cw828/p/14926751.html
Copyright © 2011-2022 走看看