Код: Выделить всё
@SpringJUnitConfig(SpringAsyncTest.MyTestConfig::class)
class SpringAsyncTest {
@Autowired
lateinit var sut: MySpecialBean
@TestConfiguration()
class MyTestConfig {
@Bean(name = ["myThreadPoolExecutor"])
fun asyncExecutor(): ThreadPoolTaskExecutor {
val executor = ThreadPoolTaskExecutor()
executor.corePoolSize = 10
executor.queueCapacity = 2000
executor.initialize()
return executor
}
@Bean
fun sut(): MySpecialBean = MySpecialBean()
}
@Test
@Throws(InterruptedException::class, ExecutionException::class)
fun testSpringAsync() {
CompletableFuture.allOf(
sut.aFutureCompletable(),
sut.aFutureCompletable(),
sut.aFutureCompletable(),
sut.aFutureCompletable(),
sut.aFutureCompletable()
).get()
}
}
Код: Выделить всё
@Component
class MySpecialBean {
@Throws(InterruptedException::class)
@Async("myThreadPoolExecutor") // Use the custom executor
fun aFutureCompletable(): CompletableFuture {
Thread.sleep(1000) // Simulate a long-running task
return CompletableFuture.completedFuture(null)
}
}
Весь код находится в проекте Gradle, который можно клонировать здесь:
github. com/fatso83/test-driven-learning/.
Подробнее здесь: https://stackoverflow.com/questions/790 ... threadpool
Мобильная версия