-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e41977
commit 6ca2bc3
Showing
4 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/corea/global/config/ThreadPoolConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package corea.global.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
@Configuration | ||
public class ThreadPoolConfig { | ||
|
||
@Bean(name = "apiExecutor") | ||
public Executor apiExecutor() { | ||
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(100); | ||
executor.setMaxPoolSize(200); | ||
executor.setQueueCapacity(50); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package corea.global.util; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
|
||
public class FutureUtil { | ||
|
||
public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier) { | ||
return CompletableFuture.supplyAsync(supplier); | ||
} | ||
|
||
public static <T> CompletableFuture<T> supplyAsync(final Supplier<T> supplier, final Executor executor) { | ||
return CompletableFuture.supplyAsync(supplier, executor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
backend/src/test/java/corea/global/util/FutureUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package corea.global.util; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class FutureUtilTest { | ||
|
||
@Test | ||
@DisplayName("지정하지 않으면 ForkedJoinPool 에서 동작한다.") | ||
void default_is_work_with_forked_join_pool() throws ExecutionException, InterruptedException { | ||
CompletableFuture<String> future = FutureUtil.supplyAsync(() -> Thread.currentThread().getName()); | ||
String threadName = future.get(); | ||
assertTrue(threadName.startsWith("ForkJoinPool")); | ||
} | ||
|
||
@Test | ||
@DisplayName("지정하면 지정한 Executors 가 제공한 스레드에서 동작한다.") | ||
void work_with_specific_executor() throws ExecutionException, InterruptedException { | ||
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setThreadNamePrefix("testExecutor"); | ||
executor.initialize(); | ||
|
||
CompletableFuture<String> future = FutureUtil.supplyAsync(() -> Thread.currentThread().getName(), executor); | ||
String threadName = future.get(); | ||
assertTrue(threadName.startsWith("testExecutor")); | ||
} | ||
} |