Skip to content

Commit

Permalink
Merge pull request #1246 from fededonna
Browse files Browse the repository at this point in the history
* pr/1246:
  Polish "Expose prestartAllCoreThreads on ExecutorService"
  Expose prestartAllCoreThreads on ExecutorService

Closes gh-1246
  • Loading branch information
snicoll committed Nov 24, 2021
2 parents 2271b60 + 19a8b94 commit 6f6d27f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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 Down Expand Up @@ -75,6 +75,8 @@ public class ThreadPoolExecutorFactoryBean extends ExecutorConfigurationSupport

private boolean allowCoreThreadTimeOut = false;

private boolean prestartAllCoreThreads = false;

private int queueCapacity = Integer.MAX_VALUE;

private boolean exposeUnconfigurableExecutor = false;
Expand Down Expand Up @@ -118,6 +120,16 @@ public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
}

/**
* Specify whether to start all core threads, causing them to idly wait for work.
* <p>Default is "false".
* @since 5.3.14
* @see java.util.concurrent.ThreadPoolExecutor#prestartAllCoreThreads
*/
public void setPrestartAllCoreThreads(boolean prestartAllCoreThreads) {
this.prestartAllCoreThreads = prestartAllCoreThreads;
}

/**
* Set the capacity for the ThreadPoolExecutor's BlockingQueue.
* Default is {@code Integer.MAX_VALUE}.
Expand Down Expand Up @@ -153,6 +165,9 @@ protected ExecutorService initializeExecutor(
if (this.allowCoreThreadTimeOut) {
executor.allowCoreThreadTimeOut(true);
}
if (this.prestartAllCoreThreads) {
executor.prestartAllCoreThreads();
}

// Wrap executor with an unconfigurable decorator.
this.exposedExecutor = (this.exposeUnconfigurableExecutor ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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 Down Expand Up @@ -94,6 +94,8 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport

private boolean allowCoreThreadTimeOut = false;

private boolean prestartAllCoreThreads = false;

@Nullable
private TaskDecorator taskDecorator;

Expand Down Expand Up @@ -197,6 +199,16 @@ public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
}

/**
* Specify whether to start all core threads, causing them to idly wait for work.
* <p>Default is "false".
* @since 5.3.14
* @see java.util.concurrent.ThreadPoolExecutor#prestartAllCoreThreads
*/
public void setPrestartAllCoreThreads(boolean prestartAllCoreThreads) {
this.prestartAllCoreThreads = prestartAllCoreThreads;
}

/**
* Specify a custom {@link TaskDecorator} to be applied to any {@link Runnable}
* about to be executed.
Expand Down Expand Up @@ -256,6 +268,9 @@ public void execute(Runnable command) {
if (this.allowCoreThreadTimeOut) {
executor.allowCoreThreadTimeOut(true);
}
if (this.prestartAllCoreThreads) {
executor.prestartAllCoreThreads();
}

this.threadPoolExecutor = executor;
return executor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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,19 +16,29 @@

package org.springframework.scheduling.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

import org.junit.jupiter.api.Test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link ThreadPoolExecutorFactoryBean}.
*
* @author Juergen Hoeller
*/
class ThreadPoolExecutorFactoryBeanTests {
Expand All @@ -44,6 +54,27 @@ void defaultExecutor() throws Exception {
context.close();
}

@Test
void executorWithDefaultSettingsDoesNotPrestartAllCoreThreads() {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean("taskExecutor", ThreadPoolExecutorFactoryBean.class, TestThreadPoolExecutorFactoryBean::new);
context.refresh();
ThreadPoolExecutor threadPoolExecutor = context.getBean(ThreadPoolExecutor.class);
verify(threadPoolExecutor, never()).prestartAllCoreThreads();
}

@Test
void executorWithPrestartAllCoreThreads() {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean("taskExecutor", ThreadPoolExecutorFactoryBean.class, () -> {
TestThreadPoolExecutorFactoryBean factoryBean = new TestThreadPoolExecutorFactoryBean();
factoryBean.setPrestartAllCoreThreads(true);
return factoryBean;
});
context.refresh();
ThreadPoolExecutor threadPoolExecutor = context.getBean(ThreadPoolExecutor.class);
verify(threadPoolExecutor).prestartAllCoreThreads();
}

@Configuration
static class ExecutorConfig {
Expand All @@ -55,4 +86,15 @@ ThreadPoolExecutorFactoryBean executor() {

}

private static class TestThreadPoolExecutorFactoryBean extends ThreadPoolExecutorFactoryBean {

@Override
protected ThreadPoolExecutor createExecutor(
int corePoolSize, int maxPoolSize, int keepAliveSeconds, BlockingQueue<Runnable> queue,
ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {

return mock(ThreadPoolExecutor.class);
}
}

}

0 comments on commit 6f6d27f

Please sign in to comment.