BAEL-3817 - Add unit tests showing the difference between the Spring ThreadPoolTaskExecutor's corePoolSize and maxPoolSize properties
This commit is contained in:
parent
63d394d697
commit
f7697918f2
|
@ -0,0 +1,12 @@
|
|||
RemoteSystemsTempFiles/
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
bin/
|
||||
.metadata/
|
||||
docs/*.autosave
|
||||
.recommenders/
|
||||
build/
|
||||
.gradle/
|
||||
.DS_Store
|
||||
.idea/
|
|
@ -0,0 +1,10 @@
|
|||
## Spring Threads
|
||||
|
||||
This module contains articles about threading using threads, specifically Spring's ThreadPoolTaskExecutor
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to Thread Pools in Java](https://www.baeldung.com/thread-pool-java-and-guava)
|
||||
- [How To Do @Async in Spring](https://www.baeldung.com/spring-async)
|
||||
- [A Guide to the Spring Task Scheduler](https://www.baeldung.com/spring-task-scheduler)
|
||||
- [A Guide to the Java ExecutorService](https://www.baeldung.com/java-executor-service-tutorial)
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?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>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.12.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,130 @@
|
|||
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 {
|
||||
|
||||
@Test
|
||||
public void whenUsingDefaults_thenSingleThread() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
taskExecutor.execute(() -> {
|
||||
try {
|
||||
Thread.sleep(10L * ThreadLocalRandom.current().nextLong(1, 10));
|
||||
countDownLatch.countDown();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
taskExecutor.execute(() -> {
|
||||
try {
|
||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
||||
countDownLatch.countDown();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCorePoolSizeFive_andMaxPoolSizeTen_thenFiveThreads() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(5);
|
||||
taskExecutor.setMaxPoolSize(10);
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
taskExecutor.execute(() -> {
|
||||
try {
|
||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
||||
countDownLatch.countDown();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(5, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCorePoolSizeFive_andMaxPoolSizeTen_andQueueCapacityZero_thenTenThreads() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(5);
|
||||
taskExecutor.setMaxPoolSize(10);
|
||||
taskExecutor.setQueueCapacity(0);
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
taskExecutor.execute(() -> {
|
||||
try {
|
||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
||||
countDownLatch.countDown();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCorePoolSizeFive_andMaxPoolSizeTen_andQueueCapacityTen_thenTenThreads() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(5);
|
||||
taskExecutor.setMaxPoolSize(10);
|
||||
taskExecutor.setQueueCapacity(10);
|
||||
taskExecutor.afterPropertiesSet();
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(10);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
taskExecutor.execute(() -> {
|
||||
try {
|
||||
Thread.sleep(100L * ThreadLocalRandom.current().nextLong(1, 10));
|
||||
countDownLatch.countDown();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
while (countDownLatch.getCount() > 0) {
|
||||
Assert.assertEquals(10, taskExecutor.getPoolSize());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue