Skip to content

Commit

Permalink
Polish contribution
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Jun 8, 2022
1 parent e386bdb commit 8478e8e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
Expand Up @@ -72,6 +72,8 @@
* {@link org.springframework.scheduling.concurrent.ConcurrentTaskExecutor} adapter.
*
* @author Juergen Hoeller
* @author Rémy Guihard
* @author Sam Brannen
* @since 2.0
* @see org.springframework.core.task.TaskExecutor
* @see java.util.concurrent.ThreadPoolExecutor
Expand Down Expand Up @@ -155,7 +157,7 @@ public int getMaxPoolSize() {

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

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

/**
* Return the capacity for the ThreadPoolExecutor's BlockingQueue.
* @since 5.3.21
* @see #setQueueCapacity(int)
*/
public int getQueueCapacity() {
return this.queueCapacity;
}

/**
* Specify whether to allow core threads to time out. This enables dynamic
* growing and shrinking even in combination with a non-zero queue (since
Expand Down Expand Up @@ -315,19 +326,18 @@ public int getPoolSize() {
}
return this.threadPoolExecutor.getPoolSize();
}

/**
* Return the current number of threads waiting in the queue
*/
public int getCurrentQueueSize() {
return this.getThreadPoolExecutor().getQueue().size();
}

/**
* Return the maximum capacity of the queue
*/
public int getQueueCapacity() {
return this.queueCapacity;
* Return the current queue size.
* @since 5.3.21
* @see java.util.concurrent.ThreadPoolExecutor#getQueue()
*/
public int getQueueSize() {
if (this.threadPoolExecutor == null) {
// Not initialized yet: assume no queued tasks.
return 0;
}
return this.threadPoolExecutor.getQueue().size();
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,17 +16,25 @@

package org.springframework.scheduling.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;

import org.springframework.core.task.AsyncListenableTaskExecutor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.InstanceOfAssertFactories.type;

/**
* Unit tests for {@link ThreadPoolTaskExecutor}.
*
* @author Juergen Hoeller
* @author Sam Brannen
* @since 5.0.5
*/
class ThreadPoolTaskExecutorTests extends AbstractSchedulingTaskExecutorTests {
Expand All @@ -50,8 +58,8 @@ void modifyCorePoolSizeWhileRunning() {

executor.setCorePoolSize(0);

assertThat(executor.getCorePoolSize()).isEqualTo(0);
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isEqualTo(0);
assertThat(executor.getCorePoolSize()).isZero();
assertThat(executor.getThreadPoolExecutor().getCorePoolSize()).isZero();
}

@Test
Expand Down Expand Up @@ -112,4 +120,37 @@ void modifyKeepAliveSecondsWithInvalidValueWhileRunning() {
assertThat(executor.getThreadPoolExecutor().getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(60);
}

@Test
void queueCapacityDefault() {
assertThat(executor.getQueueCapacity()).isEqualTo(Integer.MAX_VALUE);
assertThat(executor.getThreadPoolExecutor().getQueue())
.asInstanceOf(type(LinkedBlockingQueue.class))
.extracting(BlockingQueue::remainingCapacity).isEqualTo(Integer.MAX_VALUE);
}

@Test
void queueCapacityZero() {
executor.setQueueCapacity(0);
executor.afterPropertiesSet();

assertThat(executor.getQueueCapacity()).isZero();
assertThat(executor.getThreadPoolExecutor().getQueue())
.asInstanceOf(type(SynchronousQueue.class))
.extracting(BlockingQueue::remainingCapacity).isEqualTo(0);
}

@Test
void queueSize() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

assertThatIllegalStateException().isThrownBy(executor::getThreadPoolExecutor);
assertThat(executor.getQueueSize()).isZero();

executor.afterPropertiesSet();

assertThat(executor.getThreadPoolExecutor()).isNotNull();
assertThat(executor.getThreadPoolExecutor().getQueue()).isEmpty();
assertThat(executor.getQueueSize()).isZero();
}

}

0 comments on commit 8478e8e

Please sign in to comment.