young-ceo
young-ceo

Reputation: 5394

Using Spring Boot Async creates suspended threads

I am using Spring Boot 2.1.5.RELEASE, and using @Async. However, when the @Async method is executed, it creates a new suspended thread every time.

Configuration:

@Configuration
@EnableAsync
public class MyConfiguration implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("Async-");
        executor.setCorePoolSize(100);
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (throwable, method, objects) -> {
            logger.error("Error: ", throwable);
        };
    }
}

Implementation:

...
@Async
@Transactional
public Integer insertToTable(Foo foo) {
    // Table insert logic ...
    return CompletableFuture.completedFuture(1).join();
}
...

But when the method insertToTable is triggered, it adds a suspended thread:

Suspended Threads

Is my setup is wrong? Please help me out!

Upvotes: 0

Views: 684

Answers (1)

Almas Abdrazak
Almas Abdrazak

Reputation: 3632

It's expected behavior. Thread pool creates a new thread up to max pool size. When thread completes it task it waits for keepAliveSeconds in order to be reused(which can be configured using setKeepAliveSeconds) and then thread is stopped. By default threads wait for 1 minutes (60 seconds)

Upvotes: 2

Related Questions