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()執行