Skip to content

Commit 8478e8e

Browse files
committed
Polish contribution
This commit: - fixes Checkstyle violations - improves Javadoc - adds missing @SInCE tags - renames getCurrentQueueSize() to getQueueSize() - avoids NullPointerExceptions in getQueueSize() - introduces tests for queue size and queue capacity Closes gh-28583
1 parent e386bdb commit 8478e8e

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
* {@link org.springframework.scheduling.concurrent.ConcurrentTaskExecutor} adapter.
7373
*
7474
* @author Juergen Hoeller
75+
* @author Rémy Guihard
76+
* @author Sam Brannen
7577
* @since 2.0
7678
* @see org.springframework.core.task.TaskExecutor
7779
* @see java.util.concurrent.ThreadPoolExecutor
@@ -155,7 +157,7 @@ public int getMaxPoolSize() {
155157

156158
/**
157159
* Set the ThreadPoolExecutor's keep-alive seconds.
158-
* Default is 60.
160+
* <p>Default is 60.
159161
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
160162
*/
161163
public void setKeepAliveSeconds(int keepAliveSeconds) {
@@ -178,7 +180,7 @@ public int getKeepAliveSeconds() {
178180

179181
/**
180182
* Set the capacity for the ThreadPoolExecutor's BlockingQueue.
181-
* Default is {@code Integer.MAX_VALUE}.
183+
* <p>Default is {@code Integer.MAX_VALUE}.
182184
* <p>Any positive value will lead to a LinkedBlockingQueue instance;
183185
* any other value will lead to a SynchronousQueue instance.
184186
* @see java.util.concurrent.LinkedBlockingQueue
@@ -188,6 +190,15 @@ public void setQueueCapacity(int queueCapacity) {
188190
this.queueCapacity = queueCapacity;
189191
}
190192

193+
/**
194+
* Return the capacity for the ThreadPoolExecutor's BlockingQueue.
195+
* @since 5.3.21
196+
* @see #setQueueCapacity(int)
197+
*/
198+
public int getQueueCapacity() {
199+
return this.queueCapacity;
200+
}
201+
191202
/**
192203
* Specify whether to allow core threads to time out. This enables dynamic
193204
* growing and shrinking even in combination with a non-zero queue (since
@@ -315,19 +326,18 @@ public int getPoolSize() {
315326
}
316327
return this.threadPoolExecutor.getPoolSize();
317328
}
318-
319-
/**
320-
* Return the current number of threads waiting in the queue
321-
*/
322-
public int getCurrentQueueSize() {
323-
return this.getThreadPoolExecutor().getQueue().size();
324-
}
325329

326330
/**
327-
* Return the maximum capacity of the queue
328-
*/
329-
public int getQueueCapacity() {
330-
return this.queueCapacity;
331+
* Return the current queue size.
332+
* @since 5.3.21
333+
* @see java.util.concurrent.ThreadPoolExecutor#getQueue()
334+
*/
335+
public int getQueueSize() {
336+
if (this.threadPoolExecutor == null) {
337+
// Not initialized yet: assume no queued tasks.
338+
return 0;
339+
}
340+
return this.threadPoolExecutor.getQueue().size();
331341
}
332342

333343
/**

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,25 @@
1616

1717
package org.springframework.scheduling.concurrent;
1818

19+
import java.util.concurrent.BlockingQueue;
20+
import java.util.concurrent.LinkedBlockingQueue;
21+
import java.util.concurrent.SynchronousQueue;
1922
import java.util.concurrent.TimeUnit;
2023

2124
import org.junit.jupiter.api.Test;
2225

2326
import org.springframework.core.task.AsyncListenableTaskExecutor;
2427

2528
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2630
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31+
import static org.assertj.core.api.InstanceOfAssertFactories.type;
2732

2833
/**
34+
* Unit tests for {@link ThreadPoolTaskExecutor}.
35+
*
2936
* @author Juergen Hoeller
37+
* @author Sam Brannen
3038
* @since 5.0.5
3139
*/
3240
class ThreadPoolTaskExecutorTests extends AbstractSchedulingTaskExecutorTests {
@@ -50,8 +58,8 @@ void modifyCorePoolSizeWhileRunning() {
5058

5159
executor.setCorePoolSize(0);
5260

53-
assertThat(executor.getCorePoolSize()).isEqualTo(0);
54-
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(0);
61+
assertThat(executor.getCorePoolSize()).isZero();
62+
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isZero();
5563
}
5664

5765
@Test
@@ -112,4 +120,37 @@ void modifyKeepAliveSecondsWithInvalidValueWhileRunning() {
112120
assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60);
113121
}
114122

123+
@Test
124+
void queueCapacityDefault() {
125+
assertThat(executor.getQueueCapacity()).isEqualTo(Integer.MAX_VALUE);
126+
assertThat(executor.getThreadPoolExecutor().getQueue())
127+
.asInstanceOf(type(LinkedBlockingQueue.class))
128+
.extracting(BlockingQueue::remainingCapacity).isEqualTo(Integer.MAX_VALUE);
129+
}
130+
131+
@Test
132+
void queueCapacityZero() {
133+
executor.setQueueCapacity(0);
134+
executor.afterPropertiesSet();
135+
136+
assertThat(executor.getQueueCapacity()).isZero();
137+
assertThat(executor.getThreadPoolExecutor().getQueue())
138+
.asInstanceOf(type(SynchronousQueue.class))
139+
.extracting(BlockingQueue::remainingCapacity).isEqualTo(0);
140+
}
141+
142+
@Test
143+
void queueSize() {
144+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
145+
146+
assertThatIllegalStateException().isThrownBy(executor::getThreadPoolExecutor);
147+
assertThat(executor.getQueueSize()).isZero();
148+
149+
executor.afterPropertiesSet();
150+
151+
assertThat(executor.getThreadPoolExecutor()).isNotNull();
152+
assertThat(executor.getThreadPoolExecutor().getQueue()).isEmpty();
153+
assertThat(executor.getQueueSize()).isZero();
154+
}
155+
115156
}

0 commit comments

Comments
 (0)