Which of the following code snippets correctly implements this configuration?
hard📝 state output Q15 of 15
Spring Boot - Async Processing
You want to create a custom thread pool in Spring Boot that has a core pool size of 4, max pool size of 8, and a queue capacity of 20. You also want to set a thread name prefix "MyApp-" for easier debugging. Which of the following code snippets correctly implements this configuration?
A@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("MyApp-");
executor.initialize();
return executor;
}
B@Bean
public Executor taskExecutor() {
return Executors.newFixedThreadPool(8);
}
C@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("MyApp-");
executor.initialize();
return executor;
}
D@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("App-");
executor.initialize();
return executor;
}
Step-by-Step Solution
Solution:
Step 1: Verify core, max pool sizes and queue capacity
Core pool size 4, max pool size 8, and queue capacity 20 match the requirement in @Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("MyApp-");
executor.initialize();
return executor;
}.
Step 2: Check thread name prefix correctness
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("MyApp-");
executor.initialize();
return executor;
} sets thread name prefix to "MyApp-" exactly as requested.
Step 3: Validate other options
@Bean
public Executor taskExecutor() {
return Executors.newFixedThreadPool(8);
} uses fixed thread pool without prefix; @Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("MyApp-");
executor.initialize();
return executor;
} reverses core and max sizes (invalid); @Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("App-");
executor.initialize();
return executor;
} uses wrong prefix.
Final Answer:
Option A code snippet correctly implements the required configuration -> Option A
Quick Check:
Correct sizes and prefix = @Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(20);
executor.setThreadNamePrefix("MyApp-");
executor.initialize();
return executor;
} [OK]
Quick Trick:Match core, max sizes and prefix exactly [OK]
Common Mistakes:
Swapping core and max pool sizes
Using wrong thread name prefix
Using fixed thread pool instead of ThreadPoolTaskExecutor
Master "Async Processing" in Spring Boot
9 interactive learning modes - each teaches the same concept differently