Merge pull request #8723 from markathomas/BAEL-3817
BAEL-3817 - ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
This commit is contained in:
commit
773fe0ab8a
1
pom.xml
1
pom.xml
|
@ -728,6 +728,7 @@
|
|||
<module>spring-static-resources</module>
|
||||
<module>spring-swagger-codegen</module>
|
||||
|
||||
<module>spring-threads</module>
|
||||
<module>spring-thymeleaf</module>
|
||||
<module>spring-thymeleaf-2</module>
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-threads</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-threads</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,98 @@
|
|||
package com.baeldung.threading;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
public class ThreadPoolTaskExecutorUnitTest {
|
||||
|
||||
void startThreads(ThreadPoolTaskExecutor taskExecutor, CountDownLatch countDownLatch, int numThreads) {
|
||||
for (int i = 0; i < numThreads; i++) {
|
||||
taskExecutor.execute(() -> {
|
||||
try {
|
||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
||||
countDownLatch.countDown();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingDefaults_thenSingleThread() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(1, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCorePoolSizeFive_thenFiveThreads() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(5);
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCorePoolSizeFiveAndMaxPoolSizeTen_thenFiveThreads() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(5);
|
||||
taskExecutor.setMaxPoolSize(10);
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCorePoolSizeFiveAndMaxPoolSizeTenAndQueueCapacityZero_thenTenThreads() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(5);
|
||||
taskExecutor.setMaxPoolSize(10);
|
||||
taskExecutor.setQueueCapacity(0);
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
this.startThreads(taskExecutor, countDownLatch, 10);
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCorePoolSizeFiveAndMaxPoolSizeTenAndQueueCapacityTen_thenTenThreads() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(5);
|
||||
taskExecutor.setMaxPoolSize(10);
|
||||
taskExecutor.setQueueCapacity(10);
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(20);
|
||||
this.startThreads(taskExecutor, countDownLatch, 20);
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue