SpringBoot开始多线程
增加配置类
package com.springbootdemo.demo.config;
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.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ThreadPoolConfig {
/**
* 核心线程数
*/
;
/**
* 最大线程数
*/
;
/**
* 队列数
*/
;
@Bean
public ExecutorService testFxbDrawExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("test-fxb-draw-service-");
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 执行初始化
executor.initialize();
return executor.getThreadPoolExecutor();
}
}
示例
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements OrderMasterService {
@Resource(name = "testFxbDrawExecutor")
private ExecutorService threadPool;
public void test() throws InterruptedException {
CountDownLatch downLatch = );//配置可用线程数
; i < ; i++) {
threadPool.submit(() -> {
System.out.println(Thread.currentThread().getName() + "执行完成");
downLatch.countDown();//减少可用线程数
});
}
downLatch.await();//等待多线程全部结束
System.out.println("所有的线程执行完成");
}
}
SpringBoot开始多线程的更多相关文章
- springboot下多线程开发注意事项
基于springboot的多线程程序开发过程中,由于本身也需要注入spring容器进行管理,才能发挥springboot的优势.所以这篇文字主要用来记录开发中两者结合时需要注意的一些事项. 第一步我们 ...
- SpringBoot Test 多线程报错:dataSource already closed
1:前言 最近在项目中使用多线程对大任务拆分处理时,进行数据库操作的时候报错了. 业务代码大概是这样的: @Service public calss TestServiceImpl implement ...
- springboot开启多线程配置
一.配置线程池参数 @EnableAsync @Configuration public class TaskExecutorConfig { @Bean public TaskExecutor ta ...
- 多线程的使用(springboot)
预备知识 业务使用多线程的原因 目的是面对高并发的时候,提高运行速度 场景一: 一个业务逻辑有很多次的循环,每次循环之间没有影响,比如验证1万条url路径是否存在,正常情况要循环1万次,逐个去验证每一 ...
- springboot 学习之路 6(定时任务)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- Java并发编程--基础进阶高级(完结)
Java并发编程--基础进阶高级完整笔记. 这都不知道是第几次刷狂神的JUC并发编程了,从第一次的迷茫到现在比较清晰,算是个大进步了,之前JUC笔记不见了,重新做一套笔记. 参考链接:https:// ...
- springboot 多线程执行
一.springboot开线程执行异步任务 1.Spring通过任务执行器TaskExecutor,来实现多线程和并发编程,使用ThreadPoolTaskExecutor可实现一个基于线程池的Tas ...
- 关于java基础、多线程、JavaWeb基础、数据库、SSM、Springboot技术汇总
作者 : Stanley 罗昊 本人自行总结,纯手打,有疑问请在评论区留言 [转载请注明出处和署名,谢谢!] 一.java基础 1.多态有哪些体现形式? 重写.重载 2. Overriding的是什么 ...
- 转载-SpringBoot结合线程池解决多线程问题实录;以及自己的总结
原文地址:https://blog.csdn.net/GFJ0814/article/details/92422245 看看这篇文章(继续学习):https://www.jianshu.com/p/3 ...
随机推荐
- 设置PPT版式
1.点击视图,幻灯片母版 2.选中某个PPT母版,可进行编辑
- 收集到的技术相关网址——delphi
1.DLL封装登录框架实现代码复用 https://www.cnblogs.com/wenwencao/articles/1333659.html
- 三、RabbitMQ安装
安装前准备 Linux版本信息: 发行版本:CentOS Linux release 7.5.1804 (Core) 内核版本:Linux version 3.10.0-862.el7.x86_64 ...
- Method POST, Status (canceled) error message
I have the following code which is giving me a Method POST, Status (canceled) error message: $(docum ...
- mysqli存储过程
<?php$link = mysqli_connect('localhost','root','','chinatupai'); $sql = "call getEmail('000 ...
- JavaScript学习总结(六)数据类型和JSON格式
转自:http://segmentfault.com/a/1190000000668072 什么是JSON JSON:JavaScript 对象表示法(JavaScript Object Notati ...
- win10安装MAYA失败,怎么强力卸载删除注册表并重新安装
一些搞设计的朋友在win10系统下安装MAYA失败或提示已安装,也有时候想重新安装MAYA的时候会出现本电脑windows系统已安装MAYA,你要是不留意直接安装MAYA,只会安装MAYA的附件或者直 ...
- Java IO: OutputStream
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) OutputStream类是Java IO API中所有输出流的基类.子类包括Buffere ...
- Java IO: Reader And Writer
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) Java IO的Reader和Writer除了基于字符之外,其他方面都与InputStre ...
- id0-rsa WP合集
忙里偷闲做做题wwwwwwwwwwwww Intro to Hashing Intro to PGP Hello PGP Hello OpenSSL Intro to RSA Caesar Hello ...