springboot中多线程的使用
多线程:多个线程并发的执行
1、创建配置文件
@Configuration
@EnableAsync
public class ThreadConfig {
@Bean("doSomethingExecutor")
public Executor doSomethingExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数:线程池创建时候初始化的线程数
executor.setCorePoolSize(10);
// 最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
executor.setMaxPoolSize(20);
// 缓冲队列:用来缓冲执行任务的队列
executor.setQueueCapacity(500);
// 允许线程的空闲时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁
executor.setKeepAliveSeconds(60);
// 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
executor.setThreadNamePrefix("do-something-");
// 缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程)
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
executor.initialize();
return executor;
}
}
2、创建线程任务
- ThreadTaskService
public interface ThreadTestService {
// 无返回值的线程任务
void threadTask(int i);
// 有返回值的线程任务
CompletableFuture<String> threadTaskWithReturn01();
CompletableFuture<String> threadTaskWithReturn02();
CompletableFuture<String> threadTaskWithReturn03();
CompletableFuture<String> threadTaskWithReturn04();
}
- ThreadTaskServiceImp
import com.baizhou.service.ThreadTestService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
@Service
@EnableAsync //开启线程
public class ThreadTestServiceImp implements ThreadTestService {
@Override
@Async(value = "doSomethingExecutor") //声明线程任务
public void threadTask(int i) {
System.out.println("线程:"+Thread.currentThread().getName()+":"+i);
}
@Override
@Async(value = "doSomethingExecutor") //声明线程任务
public CompletableFuture<String> threadTaskWithReturn01() {
return CompletableFuture.completedFuture(Thread.currentThread().getName()+":threadTaskWithReturn01()执行");
}
@Override
@Async(value = "doSomethingExecutor") //声明线程任务
public CompletableFuture<String> threadTaskWithReturn02() {
return CompletableFuture.completedFuture(Thread.currentThread().getName()+":threadTaskWithReturn02()执行");
}
@Override
@Async(value = "doSomethingExecutor") //声明线程任务
public CompletableFuture<String> threadTaskWithReturn03() {
return CompletableFuture.completedFuture(Thread.currentThread().getName()+":threadTaskWithReturn03()执行");
}
@Override
@Async(value = "doSomethingExecutor") //声明线程任务
public CompletableFuture<String> threadTaskWithReturn04() {
return CompletableFuture.completedFuture(Thread.currentThread().getName()+":threadTaskWithReturn04()执行");
}
}
3、测试
@SpringBootTest
class SpringbootMybatisPlusApplicationTests02 {
@Autowired
private ThreadTestService threadTestService;
@Test
void ThreadTest(){
long start=System.currentTimeMillis();
for(int i=0;i<10;i++){
try{
Thread.sleep(1000);
threadTestService.threadTask(i);//执行线程任务
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
@Test
void ThreadTest02() throws ExecutionException, InterruptedException {
// 执行线程任务
CompletableFuture<String> doSomeThing01=threadTestService.threadTaskWithReturn01();
CompletableFuture<String> doSomeThing02=threadTestService.threadTaskWithReturn02();
CompletableFuture<String> doSomeThing03=threadTestService.threadTaskWithReturn03();
CompletableFuture<String> doSomeThing04=threadTestService.threadTaskWithReturn04();
// 等待所有的线程都执行完毕
CompletableFuture.allOf(doSomeThing01,doSomeThing02,doSomeThing03,doSomeThing04);
//获取每个任务的返回结果
System.out.println(doSomeThing01.get());
System.out.println(doSomeThing02.get());
System.out.println(doSomeThing03.get());
System.out.println(doSomeThing04.get());
}
}
- 结果
// 不带返回值
线程:do-something-1:0
线程:do-something-2:1
线程:do-something-3:2
线程:do-something-4:3
线程:do-something-5:4
线程:do-something-6:5
线程:do-something-7:6
线程:do-something-8:7
线程:do-something-9:8
线程:do-something-10:9
//带返回值
do-something-1:threadTaskWithReturn01()执行
do-something-2:threadTaskWithReturn02()执行
do-something-3:threadTaskWithReturn03()执行
do-something-4:threadTaskWithReturn04()执行