개발/Java, Spring
Spring Boot 스레드 사용 동기 처럼 사용하기.
거만떡볶이
2024. 5. 14. 14:52
1. CompletableFuture.supplyAsync 사용하여 동기 처럼 사용 도전(결과 완료를 확인하고 진행)
2. 난 공통 스레드풀을 많들어 사용 예정이다. 그래서 공통 쓰레드풀 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
@EnableAsync
@Configuration
public class AsyncConfig extends AsyncConfigurerSupport {
@Bean("threadPoolTaskExecutorA")
public Executor threadPoolTaskExecutorMember() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(99999);
executor.setThreadNamePrefix("a-thread-");
return executor;
}
@Bean("threadPoolTaskExecutorB")
public Executor threadPoolTaskExecutorBill() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(99999);
executor.setThreadNamePrefix("b-thread-");
return executor;
}
}
|
cs |
3. 스레드로 메서드 실행
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private final Executor threadPoolTaskExecutorA;
private final Executor threadPoolTaskExecutorB;
// 스레드 처리 요청목록(ThreadVO: 메서드의 리턴타입)
List<CompletableFuture<ThreadVO>> futures = new ArrayList<>();
// 스레드 메서드 실행 처리
CompletableFuture<ThreadVO> future = CompletableFuture.supplyAsync(() ->
this.insertAAAAAProc(excelRows, custPaymentsServiceFlag, custInfo, isAutoMemNoMode, custUniqNo
, custUsrUniqNo, usrId, isAllMember, rowIdxf, excelProgressCount, excelProcessId)
, threadPoolTaskExecutorA);
futures.add(future);
// 스레드 메소드 실행결과
// allOf, join() 을 하면 스레드가 결과가 모두 콜백 될때까지 기다림.
List<ThreadVO> result = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(v -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()))
.join();
|
cs |
4. 이렇게 진행을 했는데. 음. 공통 스레드를 사용하다 보니 스레드가 처리를 못하면 뒤에 들어온 업무는 큐에 계속 쌓이고
이미 큐에 많이 있는 경우 신규 등록 시 너무 오래 기다림 ㅠㅠ
5. 공통 스레드를 사용하지 않고 등록이 들어오면 스레드를 신규 생성하고 끝나면 지우는 걸로 변경
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// 큐가 쌓이는 곳
BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();
// 스레드 풀 생성
threadPoolExecutor = new ThreadPoolExecutor(3, 3, 3, TimeUnit.SECONDS, blockingQueue);
// 스레드 처리 요청목록(ThreadVO: 메서드의 리턴타입)
List<CompletableFuture<ThreadVO>> futures = new ArrayList<>();
// 스레드 메서드 실행 처리
// 끝에 새로 생긴 스레드풀에 하라고 설정
CompletableFuture<ThreadVO> future = CompletableFuture.supplyAsync(() ->
this.insertAAAAAProc(excelRows, custPaymentsServiceFlag, custInfo, isAutoMemNoMode, custUniqNo
, custUsrUniqNo, usrId, isAllMember, rowIdxf, excelProgressCount, excelProcessId)
, threadPoolExecutor);
futures.add(future);
// 스레드 메소드 실행결과
// allOf, join() 을 하면 스레드가 모무 끝날 때까지 기다림.
List<ThreadVO> result = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(v -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()))
.join();
// 스레드풀 정리
threadPoolExecutor.shutdown();
|
cs |
맞는건지 모르겠으나... 모 이렇게.....
스레드 생성시 많은 비용이 든다고 하는데 모름(무시, 모르는척?)!
JAVA 21에서 Virtual thread 라는 좋은 기능이 들어 왔다고 합니다.
전 21버전이 아니라. ㅜㅜ
https://devel-repository.tistory.com/71
spring boot 가상 스레드 적용해보기
JDK 21부터 가상 스레드(virtual thread)가 정식 릴리즈 되었다. 또한 spring boot 3.2부터는 가상 스레드가 지원되어 spring boot 애플리케이션에서 가상 스레드를 시험해 볼 수 있게 되었다. 이번 포스팅에
devel-repository.tistory.com
반응형