SpringBoot–基础–07–多线程
一、方式1
1.1、代码和测试
TaskConfig
package fei.zhou.springboot4.demo2;
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
package fei.zhou.springboot4.demo2;
/**
* 描述该类- 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
package fei.zhou.springboot4.demo2;
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
package fei.zhou.springboot4.demo2;
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";
}
}
二、方式2
2.1、代码和测试
DemoService2
package fei.zhou.springboot4.demo3;
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 DemoService2 {
@Async // 1声明方法为异步方法
public void asyncTask1(Integer i) {
try {
// 间隔1秒
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行异步任务A: " + i);
}
}
TaskTest
package fei.zhou.springboot4.demo3;
import java.util.concurrent.Executor;
import fei.zhou.springboot4.demo2.DemoService;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
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
@ComponentScan("fei.zhou.springboot4.demo3")
@EnableAsync // 1 开启异步
public class TaskTest 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;
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskTest.class);
DemoService2 demoService = context.getBean(DemoService2.class);
for (int i = 0; i < 10; i++) {
demoService.asyncTask1(i);
}
context.close();
}
}
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/mysql_mybaties_DB/springboot-learn/springboot-4