Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of configuring a custom thread pool in a Spring Boot application?
easy
A. To change the database connection settings
B. To control the number of threads used for asynchronous tasks
C. To modify the HTTP request timeout
D. To customize the logging format

Solution

  1. Step 1: Understand thread pools in Spring Boot

    Thread pools manage how many threads run tasks concurrently, improving performance and resource use.
  2. Step 2: Identify the role of custom thread pools

    Custom thread pools let you set limits on thread count and behavior for asynchronous processing.
  3. Final Answer:

    To control the number of threads used for asynchronous tasks -> Option B
  4. Quick Check:

    Thread pool purpose = control threads [OK]
Hint: Thread pools manage threads for async tasks [OK]
Common Mistakes:
  • Confusing thread pool with database or HTTP settings
  • Thinking thread pools affect logging
  • Assuming thread pools are for UI rendering
2. Which of the following is the correct way to define a custom thread pool bean in Spring Boot using Java configuration?
easy
A. @Bean public Executor taskExecutor() { return Executors.newFixedThreadPool(5); }
B. @Bean public Executor taskExecutor() { return new ThreadPoolExecutor(); }
C. @Bean public Executor taskExecutor() { return new ScheduledThreadPoolExecutor(10); }
D. @Bean public Executor taskExecutor() { return new ThreadPoolTaskExecutor(); }

Solution

  1. Step 1: Recall Spring Boot thread pool bean creation

    Spring Boot uses ThreadPoolTaskExecutor for custom thread pools, which integrates with Spring's task execution.
  2. Step 2: Analyze options

    @Bean public Executor taskExecutor() { return new ThreadPoolTaskExecutor(); } correctly returns a new ThreadPoolTaskExecutor instance, which is the recommended way.
  3. Final Answer:

    @Bean public Executor taskExecutor() { return new ThreadPoolTaskExecutor(); } -> Option D
  4. Quick Check:

    Spring thread pool bean = ThreadPoolTaskExecutor [OK]
Hint: Use ThreadPoolTaskExecutor for Spring thread pools [OK]
Common Mistakes:
  • Using Executors.newFixedThreadPool directly without Spring wrapper
  • Trying to instantiate ThreadPoolExecutor without parameters
  • Confusing scheduled thread pools with normal thread pools
3. Given this Spring Boot configuration snippet, what will be the maximum number of threads allowed in the pool?
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  executor.setCorePoolSize(3);
  executor.setMaxPoolSize(6);
  executor.setQueueCapacity(10);
  executor.initialize();
  return executor;
}
medium
A. 6
B. 3
C. 10
D. 13

Solution

  1. Step 1: Understand core and max pool size

    Core pool size is minimum threads kept alive; max pool size is the maximum threads allowed.
  2. Step 2: Identify maxPoolSize value

    The code sets maxPoolSize to 6, so the pool can grow up to 6 threads.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Max threads = maxPoolSize = 6 [OK]
Hint: Max threads = maxPoolSize property [OK]
Common Mistakes:
  • Confusing queue capacity with max threads
  • Thinking corePoolSize is max threads
  • Adding corePoolSize and queueCapacity incorrectly
4. What is wrong with this custom thread pool configuration in Spring Boot?
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  executor.setCorePoolSize(5);
  executor.setMaxPoolSize(3);
  executor.setQueueCapacity(10);
  executor.initialize();
  return executor;
}
medium
A. Missing @EnableAsync annotation on configuration class
B. Queue capacity cannot be set in ThreadPoolTaskExecutor
C. Core pool size is greater than max pool size, which is invalid
D. ThreadPoolTaskExecutor cannot be returned as a bean

Solution

  1. Step 1: Check core and max pool size relationship

    Core pool size must be less than or equal to max pool size; here core is 5 but max is 3, which is invalid.
  2. Step 2: Validate other settings

    Queue capacity is valid; @EnableAsync is optional but recommended; returning ThreadPoolTaskExecutor bean is correct.
  3. Final Answer:

    Core pool size is greater than max pool size, which is invalid -> Option C
  4. Quick Check:

    Core pool ≤ max pool required [OK]
Hint: Core pool size must not exceed max pool size [OK]
Common Mistakes:
  • Setting core pool size larger than max pool size
  • Assuming queue capacity is mandatory to set
  • Forgetting to add @EnableAsync (not always required)
5. 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?
hard
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; }

Solution

  1. 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; }.
  2. 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.
  3. 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.
  4. Final Answer:

    Option A code snippet correctly implements the required configuration -> Option A
  5. 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]
Hint: 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard