Skip to content

Commit 5311742

Browse files
filiphrjhoeller
authored andcommitted
Apply dynamic changes in ThreadPoolTaskExecutor before setting local value
If the ThreadPoolTaskExecutor is dynamically changed with an invalid value the state of the ThreadPoolTaskExecutor does no longer correctly represent the state of the underlying ThreadPoolExecutor
1 parent 564c6f7 commit 5311742

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
112112
*/
113113
public void setCorePoolSize(int corePoolSize) {
114114
synchronized (this.poolSizeMonitor) {
115-
this.corePoolSize = corePoolSize;
116115
if (this.threadPoolExecutor != null) {
117116
this.threadPoolExecutor.setCorePoolSize(corePoolSize);
118117
}
118+
this.corePoolSize = corePoolSize;
119119
}
120120
}
121121

@@ -135,10 +135,10 @@ public int getCorePoolSize() {
135135
*/
136136
public void setMaxPoolSize(int maxPoolSize) {
137137
synchronized (this.poolSizeMonitor) {
138-
this.maxPoolSize = maxPoolSize;
139138
if (this.threadPoolExecutor != null) {
140139
this.threadPoolExecutor.setMaximumPoolSize(maxPoolSize);
141140
}
141+
this.maxPoolSize = maxPoolSize;
142142
}
143143
}
144144

@@ -158,10 +158,10 @@ public int getMaxPoolSize() {
158158
*/
159159
public void setKeepAliveSeconds(int keepAliveSeconds) {
160160
synchronized (this.poolSizeMonitor) {
161-
this.keepAliveSeconds = keepAliveSeconds;
162161
if (this.threadPoolExecutor != null) {
163162
this.threadPoolExecutor.setKeepAliveTime(keepAliveSeconds, TimeUnit.SECONDS);
164163
}
164+
this.keepAliveSeconds = keepAliveSeconds;
165165
}
166166
}
167167

spring-context/src/test/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutorTests.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,96 @@
1616

1717
package org.springframework.scheduling.concurrent;
1818

19+
import java.util.concurrent.TimeUnit;
20+
21+
import org.junit.jupiter.api.Test;
1922
import org.springframework.core.task.AsyncListenableTaskExecutor;
2023

24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
26+
2127
/**
2228
* @author Juergen Hoeller
2329
* @since 5.0.5
2430
*/
2531
class ThreadPoolTaskExecutorTests extends AbstractSchedulingTaskExecutorTests {
2632

33+
private final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
34+
2735
@Override
2836
protected AsyncListenableTaskExecutor buildExecutor() {
29-
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
3037
executor.setThreadNamePrefix(this.threadNamePrefix);
3138
executor.setMaxPoolSize(1);
3239
executor.afterPropertiesSet();
3340
return executor;
3441
}
3542

43+
@Test
44+
void modifyCorePoolSizeWhileRunning() {
45+
assertThat(executor.getCorePoolSize()).isEqualTo(1);
46+
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(1);
47+
48+
executor.setCorePoolSize(0);
49+
50+
assertThat(executor.getCorePoolSize()).isEqualTo(0);
51+
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(0);
52+
}
53+
54+
@Test
55+
void modifyCorePoolSizeWithInvalidValueWhileRunning() {
56+
assertThat(executor.getCorePoolSize()).isEqualTo(1);
57+
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(1);
58+
59+
assertThatThrownBy(() -> executor.setCorePoolSize(-1))
60+
.isInstanceOf(IllegalArgumentException.class);
61+
62+
assertThat(executor.getCorePoolSize()).isEqualTo(1);
63+
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(1);
64+
}
65+
66+
@Test
67+
void modifyMaxPoolSizeWhileRunning() {
68+
assertThat(executor.getMaxPoolSize()).isEqualTo(1);
69+
assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(1);
70+
71+
executor.setMaxPoolSize(5);
72+
73+
assertThat(executor.getMaxPoolSize()).isEqualTo(5);
74+
assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(5);
75+
}
76+
77+
@Test
78+
void modifyMaxPoolSizeWithInvalidValueWhileRunning() {
79+
assertThat(executor.getMaxPoolSize()).isEqualTo(1);
80+
assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(1);
81+
82+
assertThatThrownBy(() -> executor.setMaxPoolSize(0))
83+
.isInstanceOf(IllegalArgumentException.class);
84+
85+
assertThat(executor.getMaxPoolSize()).isEqualTo(1);
86+
assertThat(executor.getThreadPoolExecutor().getMaximumPoolSize()).isEqualTo(1);
87+
}
88+
89+
@Test
90+
void modifyKeepAliveSecondsWhileRunning() {
91+
assertThat(executor.getKeepAliveSeconds()).isEqualTo(60);
92+
assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60);
93+
94+
executor.setKeepAliveSeconds(10);
95+
96+
assertThat(executor.getKeepAliveSeconds()).isEqualTo(10);
97+
assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(10);
98+
}
99+
100+
@Test
101+
void modifyKeepAliveSecondsWithInvalidValueWhileRunning() {
102+
assertThat(executor.getKeepAliveSeconds()).isEqualTo(60);
103+
assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60);
104+
105+
assertThatThrownBy(() -> executor.setKeepAliveSeconds(-10))
106+
.isInstanceOf(IllegalArgumentException.class);
107+
108+
assertThat(executor.getKeepAliveSeconds()).isEqualTo(60);
109+
assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60);
110+
}
36111
}

0 commit comments

Comments
 (0)