SpringBoot07多线程

本文介绍了如何在SpringBoot中配置线程池并实现异步任务。通过`TaskConfig`类设置线程池参数,如核心线程数、最大线程数和阻塞队列大小。`DemoService`接口中定义了异步方法`asyncTask1`,并在`DemoServiceImpl`中实现。`DemoController`调用此异步方法并展示并发执行的结果。执行结果显示异步任务按顺序输出,验证了异步处理的正确性。
摘要由CSDN通过智能技术生成

Spring Boot多线程

TaskConfig.java
创建线程池与核心线程数,最大线程数

package com.yhw.demo;

import java.util.concurrent.Executor;

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;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: TaskConfig
 * @date 2021/1/20 11:38
 * @Verson 1.0 -2021/1/20 11:38
 * @see
 */
@Configuration
@EnableAsync //1 开启异步
public class TaskConfig implements AsyncConfigurer {
	
	@Override
	public Executor getAsyncExecutor() {
		// 设置线程池
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		
		// 核心线程数
		taskExecutor.setCorePoolSize(25);
		// 最大线程数
		taskExecutor.setMaxPoolSize(50);
		// 阻塞队列
		taskExecutor.setQueueCapacity(100);
		taskExecutor.initialize();
		return taskExecutor;
	}
	
	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}
}

异步方法的接口实现

DemoService.java

package com.yhw.demo;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: DemoService
 * @date 2021/1/20 11:42
 * @Verson 1.0 -2021/1/20 11:42
 * @see
 */
public interface DemoService {

	void asyncTask1(Integer i);
}

DemoServiceImpl.java

package com.yhw.demo;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: DemoService
 * @date 2021/1/20 11:42
 * @Verson 1.0 -2021/1/20 11:42
 * @see
 */

@Service
public class DemoServiceImpl implements DemoService {
	
	@Async // 1声明方法为异步方法
	@Override
	public void asyncTask1(Integer i) {
		try {
			//间隔1秒
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("执行异步任务A: " + i);
	}
	
}

DemoController.java

package com.yhw.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: DemoService
 * @date 2021/1/20 11:42
 * @Verson 1.0 -2021/1/20 11:42
 * @see
 */

@RestController
public class DemoController {
	@Autowired
	private DemoService demoService;
	
	@RequestMapping("t1")
	public Object t1() {
		for (int i = 1; i < 100; i++) {
			demoService.asyncTask1(i);
		}
		return "ok";
	}
}

执行结果

执行异步任务A: 5
执行异步任务A: 3
执行异步任务A: 1
执行异步任务A: 2
执行异步任务A: 12
执行异步任务A: 13
执行异步任务A: 17
执行异步任务A: 15
执行异步任务A: 4
执行异步任务A: 7
执行异步任务A: 11
执行异步任务A: 6
执行异步任务A: 8
执行异步任务A: 10
执行异步任务A: 23
执行异步任务A: 22
执行异步任务A: 9

```![在这里插入图片描述](https://img-blog.csdnimg.cn/9b643e898f4b4605b2b842101d3a9b67.png)
## 总结
使用@Async表名实现方法为异步,通过	睡眠的方式实现异步
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级无敌暴龙战士塔塔开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值