0
0
Spring Bootframework~10 mins

Custom thread pool configuration in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a thread pool bean with a fixed size of 5 threads.

Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Configuration
public class ThreadPoolConfig {

    @Bean
    public ExecutorService taskExecutor() {
        return Executors.[1](5);
    }
}
Drag options to blanks, or click blank then click option'
AnewSingleThreadExecutor
BnewCachedThreadPool
CnewScheduledThreadPool
DnewFixedThreadPool
Attempts:
3 left
💡 Hint
Common Mistakes
Using newCachedThreadPool creates a pool that can grow dynamically.
Using newSingleThreadExecutor creates only one thread, not 5.
2fill in blank
medium

Complete the code to set the core pool size of the ThreadPoolTaskExecutor to 10.

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
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize([1]);
        return executor;
    }
}
Drag options to blanks, or click blank then click option'
A15
B20
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the core pool size to a different number than requested.
Confusing core pool size with max pool size.
3fill in blank
hard

Fix the error in the code by completing the missing method to initialize the executor.

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
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.[1]();
        return executor;
    }
}
Drag options to blanks, or click blank then click option'
Ainit
Bstart
Cinitialize
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using start() or run() which are not methods of ThreadPoolTaskExecutor.
Forgetting to call initialize() causes the executor not to start properly.
4fill in blank
hard

Fill both blanks to configure the thread name prefix and queue capacity.

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
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix([1]);
        executor.setQueueCapacity([2]);
        return executor;
    }
}
Drag options to blanks, or click blank then click option'
A"MyPool-"
B"Thread-"
C25
D50
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number for thread name prefix instead of a string.
Setting queue capacity too low or forgetting to set it.
5fill in blank
hard

Fill all three blanks to set core pool size, max pool size, and keep alive seconds.

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
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize([1]);
        executor.setMaxPoolSize([2]);
        executor.setKeepAliveSeconds([3]);
        return executor;
    }
}
Drag options to blanks, or click blank then click option'
A5
B10
C60
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Setting core pool size greater than max pool size.
Using keep alive seconds as zero or negative.