Close

Spring Framework - ThreadPoolTaskExecutor Examples

Spring Framework 

This TaskExecutor is based on java.util.concurrent.ThreadPoolExecutor.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public class ThreadPoolExecutorExample {
public static void main (String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = context.getBean(MyBean.class);
bean.runTasks();
ThreadPoolTaskExecutor t = context.getBean(ThreadPoolTaskExecutor.class);
t.shutdown();
}

@Configuration
public static class MyConfig {

@Bean
MyBean myBean () {
return new MyBean();
}

@Bean
TaskExecutor taskExecutor () {
ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
t.setCorePoolSize(10);
t.setMaxPoolSize(100);
t.setQueueCapacity(50);
t.setAllowCoreThreadTimeOut(true);
t.setKeepAliveSeconds(120);
return t;
}
}

private static class MyBean {
@Autowired
private TaskExecutor executor;

public void runTasks () {
for (int i = 0; i < 10; i++) {
executor.execute(getTask(i));
}
}

private Runnable getTask (int i) {
return () -> {
System.out.printf("running task %d. Thread: %s%n",
i,
Thread.currentThread().getName());
};
}
}
}

Output

running task 0. Thread: taskExecutor-1
running task 6. Thread: taskExecutor-7
running task 5. Thread: taskExecutor-6
running task 4. Thread: taskExecutor-5
running task 3. Thread: taskExecutor-4
running task 2. Thread: taskExecutor-3
running task 1. Thread: taskExecutor-2
running task 9. Thread: taskExecutor-10
running task 8. Thread: taskExecutor-9
running task 7. Thread: taskExecutor-8
Original Post




See Also