赞
踩
在实际开发过程中,我们可能会碰到以下情况,需要调用ABC三方法,但ABC三个方法的并没有逻辑关联,允许并行的运行,这个时候可以考虑采用异步的方式分别执行三个任务, 提升代码的运行效率。如果是想了解java代码是如何实现多线程的,可以参考这篇博客文章,本篇博客主要介绍SpringBoot是如何使用多线程,通过阅读本文,你经了解以下几个知识点:
第一步:在Application启动类上面加上@EnableAsync
@SpringBootApplication
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
第二步:在需要异步执行的方法上加上@Async注解
@Service
public class AsyncDemo {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Async
public void hello(String name) throws InterruptedException {
logger.info("异步线程启动 started."+name);
// 模拟方法耗时
Thread.sleep(200);
}
}
第三步:测试类进行测试验证
@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class AsyncTest {
@Autowired
public AsyncDemo asyncDemo;
@Test
public void contextLoads() throws InterruptedException {
asyncDemo.hello("hello world");
System.out.println(Thread.currentThread().getName()+"开始运行");
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+"运行结束");
}
}

开启新线程执行,方法是异步执行,验证成功。
第一步:在application文件添加SpringBoot线程池的配置
# 核心线程数
spring.task.execution.pool.core-size=8
# 最大线程数
spring.task.execution.pool.max-size=16
# 空闲线程存活时间
spring.task.execution.pool.keep-alive=60s
# 是否允许核心线程超时
spring.task.execution.pool.allow-core-thread-timeout=true
# 线程队列数量
spring.task.execution.pool.queue-capacity=100
# 线程关闭等待
spring.task.execution.shutdown.await-termination=false
spring.task.execution.shutdown.await-termination-period=
# 线程名称前缀
spring.task.execution.thread-name-prefix=task-
第二步:需要异步调用的方法, 可以不使用@Async, 启动类上可以不添加@EnableAsync
public void hello(String name) throws InterruptedException {
//这里使用logger 方便查看执行的线程是什么
logger.info("异步线程启动 started."+name);
Thread.sleep(200);
}
第三步:测试类进行测试验证
@SpringBootTest class ThreadPoolApplicationTests { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired AsyncTest asyncTest; @Autowired ThreadPoolTaskExecutor threadPoolTaskExecutor; @Test void contextLoads() throws InterruptedException { asyncTest.hello("async注解创建"); threadPoolTaskExecutor.submit(new Thread(()->{ logger.info("threadPoolTaskExecutor 创建线程"); })); //一定要休眠 不然主线程关闭了,子线程还没有启动 Thread.sleep(1000); } }

开启新线程执行,方法是异步执行,验证成功。
第一步:创建ThreadPoolConfig 配置多个线程池。
@Configuration public class ThreadPoolConfig { @Bean("taskExecutor") public Executor taskExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); //设置线程池参数信息 taskExecutor.setCorePoolSize(10); taskExecutor.setMaxPoolSize(50); taskExecutor.setQueueCapacity(200); taskExecutor.setKeepAliveSeconds(60); taskExecutor.setThreadNamePrefix("myExecutor--"); taskExecutor.setWaitForTasksToCompleteOnShutdown(true); taskExecutor.setAwaitTerminationSeconds(60); //修改拒绝策略为使用当前线程执行 taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //初始化线程池 taskExecutor.initialize(); return taskExecutor; } @Bean("poolExecutor") public Executor poolExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); //设置线程池参数信息 taskExecutor.setCorePoolSize(10); taskExecutor.setMaxPoolSize(50); taskExecutor.setQueueCapacity(200); taskExecutor.setKeepAliveSeconds(60); taskExecutor.setThreadNamePrefix("myExecutor2--"); taskExecutor.setWaitForTasksToCompleteOnShutdown(true); taskExecutor.setAwaitTerminationSeconds(60); //修改拒绝策略为使用当前线程执行 taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //初始化线程池 taskExecutor.initialize(); return taskExecutor; } @Bean("taskPoolExecutor") public Executor taskPoolExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); //设置线程池参数信息 taskExecutor.setCorePoolSize(10); taskExecutor.setMaxPoolSize(50); taskExecutor.setQueueCapacity(200); taskExecutor.setKeepAliveSeconds(60); taskExecutor.setThreadNamePrefix("myExecutor3--"); taskExecutor.setWaitForTasksToCompleteOnShutdown(true); taskExecutor.setAwaitTerminationSeconds(60); //修改拒绝策略为使用当前线程执行 taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //初始化线程池 taskExecutor.initialize(); return taskExecutor; } }
第二步:需要异步调用的方法, 可以不使用@Async, 启动类上可以不添加@EnableAsync
public void hello(String name) throws InterruptedException {
//这里使用logger 方便查看执行的线程是什么
logger.info("异步线程启动 started."+name);
Thread.sleep(200);
}
第三步:测试类进行测试验证
@SpringBootTest(classes = DemoApplication.class) @RunWith(SpringRunner.class) public class MultipleThreadPoolTest { @Autowired AsyncDemo asyncDemo; @Autowired @Qualifier("poolExecutor") ThreadPoolTaskExecutor threadPoolTaskExecutor; @Test public void contextLoads() throws InterruptedException { System.out.println(Thread.currentThread().getName()+"开始运行"); threadPoolTaskExecutor.submit(new Thread(() -> { try { asyncDemo.hello("异步方法调用"); } catch (InterruptedException e) { throw new RuntimeException(e); } })); Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+"运行结束"); } }

然后执行之前写的测试代码发现,使用的线程池已经变成自定义的线程池了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。