Close

Spring Framework - ConcurrentTaskExecutor Examples

Spring Framework 

This TaskExecutor implementation wraps the java.util.concurrent.Executor instance. That abstracts away underlying thread executor and TaskExecutor is exposed to the client beans.

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.TaskDecorator;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ConcurrentTaskExecutorExample {
public static void main (String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = context.getBean(MyBean.class);
bean.runTasks();
ConcurrentTaskExecutor exec = context.getBean(ConcurrentTaskExecutor.class);
ExecutorService service = (ExecutorService) exec.getConcurrentExecutor();
service.shutdown();
}

@Configuration
public static class MyConfig {

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

@Bean
TaskExecutor taskExecutor () {
ConcurrentTaskExecutor t = new ConcurrentTaskExecutor(
Executors.newFixedThreadPool(3));
t.setTaskDecorator(new TaskDecorator() {
@Override
public Runnable decorate (Runnable runnable) {
return () -> {

MyTask task = (MyTask) runnable;
long t = System.currentTimeMillis();
task.run();
System.out.printf("time taken for task: %s , %s%n",
task.getI(),
(System.currentTimeMillis() - t));
};
}
});
return t;
}
}

private static class MyBean {
@Autowired
private TaskExecutor executor;

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

}
}
}

private static class MyTask implements Runnable {

private final int i;

MyTask (int i) {
this.i = i;
}

@Override
public void run () {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("running task %d. Thread: %s%n",
i,
Thread.currentThread().getName());
}


public int getI () {
return i;
}
}
}

Output

running task 2. Thread: pool-1-thread-3
time taken for task: 2 , 207
running task 1. Thread: pool-1-thread-2
time taken for task: 1 , 207
running task 0. Thread: pool-1-thread-1
time taken for task: 0 , 208
running task 4. Thread: pool-1-thread-2
time taken for task: 4 , 204
running task 3. Thread: pool-1-thread-3
time taken for task: 3 , 205
running task 5. Thread: pool-1-thread-1
time taken for task: 5 , 204
running task 8. Thread: pool-1-thread-1
time taken for task: 8 , 201
running task 7. Thread: pool-1-thread-3
time taken for task: 7 , 202
running task 6. Thread: pool-1-thread-2
time taken for task: 6 , 203
running task 9. Thread: pool-1-thread-1
time taken for task: 9 , 205
Original Post




See Also