0
0
Spring Bootframework~20 mins

Custom thread pool configuration in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Thread Pool Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Thread pool behavior with fixed size
Given a Spring Boot application with a fixed thread pool of size 3, what happens when 5 tasks are submitted simultaneously?
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class ThreadPoolConfig {
    @Bean(name = "fixedThreadPool")
    public ThreadPoolTaskExecutor fixedThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(3);
        executor.setQueueCapacity(10);
        executor.initialize();
        return executor;
    }
}
AOnly 3 tasks run, the other 2 are rejected with an exception
BAll 5 tasks run immediately in parallel
C3 tasks run immediately, 2 tasks wait in the queue until threads free up
DTasks run sequentially one after another, ignoring thread pool size
Attempts:
2 left
💡 Hint
Think about how fixed thread pools handle tasks exceeding the core pool size with a queue.
📝 Syntax
intermediate
1:30remaining
Correct syntax for custom thread pool bean
Which option correctly defines a Spring Boot ThreadPoolTaskExecutor bean with a core pool size of 5 and max pool size of 10?
A
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
B
executor.setCorePoolSize = 5;
executor.setMaxPoolSize = 10;
C
executor.corePoolSize(5);
executor.maxPoolSize(10);
D
executor.setCorePool(5);
executor.setMaxPool(10);
Attempts:
2 left
💡 Hint
Look for the correct setter method names and syntax in Java.
🔧 Debug
advanced
2:30remaining
Identify the cause of thread pool rejection
A Spring Boot app uses a ThreadPoolTaskExecutor with core size 2, max size 4, and queue capacity 2. When submitting 7 tasks, some are rejected. Why?
Spring Boot
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(2);
executor.initialize();

// 7 tasks submitted simultaneously
ACore pool size is too small causing immediate rejection
BQueue capacity is unlimited, so no rejection should occur
CMax pool size is ignored, causing rejection
DThe queue is full and max threads are busy, so extra tasks are rejected
Attempts:
2 left
💡 Hint
Consider how tasks are handled when both threads and queue are at capacity.
state_output
advanced
2:00remaining
Thread pool active count after task submission
After submitting 6 tasks to a ThreadPoolTaskExecutor with core size 3, max size 6, and queue capacity 0, what is the active thread count?
Spring Boot
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(6);
executor.setQueueCapacity(0);
executor.initialize();

// Submit 6 tasks simultaneously
// Then call executor.getActiveCount()
A0
B6
CDepends on task duration
D3
Attempts:
2 left
💡 Hint
With zero queue capacity, tasks must run immediately or be rejected.
🧠 Conceptual
expert
3:00remaining
Effect of allowing core thread timeout
What happens if you configure a ThreadPoolTaskExecutor with allowCoreThreadTimeOut(true) and a keepAliveSeconds of 10?
Spring Boot
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setKeepAliveSeconds(10);
executor.setAllowCoreThreadTimeOut(true);
executor.initialize();
ACore threads will terminate if idle for 10 seconds, reducing resource use
BCore threads never terminate regardless of idle time
CMax threads terminate after 10 seconds but core threads stay alive
DThreads terminate immediately after task completion
Attempts:
2 left
💡 Hint
Think about what allowing core thread timeout means for thread lifecycle.