int pageSize = 10000;
int totalCount = doctorDAO.selectDatasByMapCount2(jsonArray, false, null);
int m = totalCount % pageSize;
int pageCount = m == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1); List<DoctorDO> resultList = new ArrayList<>();
BlockingQueue<Future<List<DoctorDO>>> queue = new LinkedBlockingQueue<>(); for (int i = 0; i < pageCount; i++) {
Thread.sleep(0);
Future<List<DoctorDO>> future;
future = queryDatas(jsonArray, i * pageSize, pageSize);
queue.add(future);
} int queueSize = queue.size();
logger.debug("queue size:" + queueSize);
for (int i = 0; i < queueSize; i++) {
List<DoctorDO> subAttendList = queue.take().get();
if (!CollectionUtils.isEmpty(subAttendList)) {
resultList.addAll(subAttendList);
}
}
     @Async
public Future<List<DoctorDO>> queryDatas(JSONArray jsonArray, int start, int size) {
List<DoctorDO> subAttendList = doctorDAO.selectDatasByMap2(start, size, jsonArray, true, null);
logger.info("完成任务");
return new AsyncResult<>(subAttendList);
}
 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.lang.reflect.Method;
import java.util.concurrent.Executor; /**
* 多线程配置类
*/
@Configuration
@EnableAsync
public class ThreadConfig implements AsyncConfigurer {
private static Logger logger = LoggerFactory.getLogger(ThreadConfig.class); /**
* The {@link Executor} instance to be used when processing async
* method invocations.
*/
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(15);
executor.setMaxPoolSize(25);
executor.setQueueCapacity(25);
executor.initialize();
return executor;
} /**
* The {@link AsyncUncaughtExceptionHandler} instance to be used
* when an exception is thrown during an asynchronous method execution
* with {@code void} return type.
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SpringAsyncExceptionHandler();
} class SpringAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
throwable.printStackTrace();
}
} public static void main(String[] args) {
logger.info("123");
logger.info("123");
logger.info("123");
}
}

springboot 多线程的使用的更多相关文章

  1. springboot 多线程执行

    一.springboot开线程执行异步任务 1.Spring通过任务执行器TaskExecutor,来实现多线程和并发编程,使用ThreadPoolTaskExecutor可实现一个基于线程池的Tas ...

  2. SpringBoot 多线程

    Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程.使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.而实际开发中任务一般是非阻 ...

  3. spring-boot 多线程

    //配置类 package test; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import ...

  4. 玩转SpringBoot之定时任务@Scheduled线程池配置

    序言 对于定时任务,在SpringBoot中只需要使用@Scheduled 这个注解就能够满足需求,它的出现也给我们带了很大的方便,我们只要加上该注解,并且根据需求设置好就可以使用定时任务了. 但是, ...

  5. springboot下多线程开发注意事项

    基于springboot的多线程程序开发过程中,由于本身也需要注入spring容器进行管理,才能发挥springboot的优势.所以这篇文字主要用来记录开发中两者结合时需要注意的一些事项. 第一步我们 ...

  6. 关于java基础、多线程、JavaWeb基础、数据库、SSM、Springboot技术汇总

    作者 : Stanley 罗昊 本人自行总结,纯手打,有疑问请在评论区留言 [转载请注明出处和署名,谢谢!] 一.java基础 1.多态有哪些体现形式? 重写.重载 2. Overriding的是什么 ...

  7. 转载-SpringBoot结合线程池解决多线程问题实录;以及自己的总结

    原文地址:https://blog.csdn.net/GFJ0814/article/details/92422245 看看这篇文章(继续学习):https://www.jianshu.com/p/3 ...

  8. SpringBoot Test 多线程报错:dataSource already closed

    1:前言 最近在项目中使用多线程对大任务拆分处理时,进行数据库操作的时候报错了. 业务代码大概是这样的: @Service public calss TestServiceImpl implement ...

  9. SpringBoot中如何优雅的使用多线程

    SpringBoot中如何优雅的使用多线程 当异步方法有返回值时,如何获取异步方法执行的返回结果呢?这时需要异步调用的方法带有返回值CompletableFuture

随机推荐

  1. MyBatis笔记-03-CRUD

    3.CRUD 1.namespace namespace中的包名要和Dao/mapper接口的包名保持一致 2.select 选择查询语句: id:就是对应的namespace中的方法名: resul ...

  2. Mybatis XML配置(转载)

    原文地址:https://www.w3cschool.cn/mybatis/f4uw1ilx.html Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它 ...

  3. linux 计算机概论 Linux介绍

    CPU: CPU内部可以分为两个主要单元:算数逻辑单元和控制单元. 算数逻辑单元主要用于程序运算和逻辑判断,控制单元主要用于协调各个组件和各单元的工作. CPU基本可以分为两种: 精简指令集和复杂指令 ...

  4. fastclick无需对IOS11及以上做处理

    // 解决click点击300毫秒延时问题 (IOS11及以上无需处理) import FastClick from 'fastclick' const device = navigator.user ...

  5. Python之exec()/compile()方法使用

    # Python内置函数exec()可以用来执行Python代码 # 或内置函数compile()编译的代码对象 # exec程序中可写入python语法格式的代码,并直接输出. exec('prin ...

  6. Acwing-202-最幸运的数字(同余, 欧拉定理)

    链接: https://www.acwing.com/problem/content/204/ 题意: 8是中国的幸运数字,如果一个数字的每一位都由8构成则该数字被称作是幸运数字. 现在给定一个正整数 ...

  7. 题解 【NOIP2016】组合数问题

    [NOIP2016]组合数问题 Description Input 第一行有两个整数t, k,其中t代表该测试点总共有多少组测试数据,k的意义见[问题描述]. 接下来t行每行两个整数n, m,其中n, ...

  8. SparkSQL之UDAF使用

    1.创建一个类继承UserDefinedAggregateFunction类. ------------------------------------------------------------ ...

  9. 上传项目到码云或GitHub

    一.安装Git 官网下载地址:https://git-scm.com/download/win 安装完成后,配置环境变量即可, 打开cmd,输入 git,出现以下提示即表示安装成功: 二.生成ssh公 ...

  10. JVM(五),ClassLoader

    五.ClassLoader 1.什么是ClassLoader 2.四种ClassLoader 3.自定义CLassLoader (1)MyClassLoader public class MyClas ...