Close

Spring Framework - SimpleAsyncTaskExecutor Examples

Spring Framework 

This TaskExecutor implementation creates a new Thread for each task submission.

package com.logicbig.example;

import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;

public class TaskExecutorStandAloneExample {

public static void main (String... strings) {
TaskExecutor theExecutor = new SimpleAsyncTaskExecutor();

theExecutor.execute(new Runnable() {
@Override
public void run () {
System.out.println("running task in thread: "
+ Thread.currentThread()
.getName());
}
});

System.out.println("in main thread: " + Thread.currentThread()
.getName());

}
}

Output

in main thread: com.logicbig.example.TaskExecutorStandAloneExample.main()
running task in thread: SimpleAsyncTaskExecutor-1
Original Post




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.AsyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class AsyncTaskExecutorExample {
public static void main (String[] args) throws Exception {
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = context.getBean(MyBean.class);
bean.runTasks();
}

@Configuration
public static class MyConfig {

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

@Bean
AsyncTaskExecutor taskExecutor () {
SimpleAsyncTaskExecutor t = new SimpleAsyncTaskExecutor();
t.setConcurrencyLimit(100);
return t;
}
}

private static class MyBean {
@Autowired
private AsyncTaskExecutor executor;

public void runTasks () throws Exception {
List<Future<?>> futureList = new ArrayList<>();

for (int i = 0; i < 10; i++) {
Future<?> future = executor.submit(getTask(i));
futureList.add(future);
}

for (Future<?> future : futureList) {
System.out.println(future.get());
}
}

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

Output

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




In Spring 6.0 AsyncListenableTaskExecutor has been deprecated in favor of AsyncTaskExecutor's method which return CompletableFuture.

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.AsyncListenableTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;

public class AsyncTaskExecutorCompletableFutureExample {

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

@Configuration
public static class MyConfig {

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

@Bean
AsyncListenableTaskExecutor taskExecutor() {
SimpleAsyncTaskExecutor t = new SimpleAsyncTaskExecutor();
t.setConcurrencyLimit(100);
return t;
}
}

private static class MyBean {
@Autowired
private AsyncListenableTaskExecutor executor;

public void runTasks() throws Exception {

for (int i = 0; i < 10; i++) {
CompletableFuture<String> completableFuture =
executor.submitCompletable(getTask(i));
completableFuture.thenAccept(task -> System.out.println("Completed: " + task));
}
}

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


}

Output

running task 0. Thread: SimpleAsyncTaskExecutor-1
running task 1. Thread: SimpleAsyncTaskExecutor-2
running task 2. Thread: SimpleAsyncTaskExecutor-3
running task 3. Thread: SimpleAsyncTaskExecutor-4
running task 5. Thread: SimpleAsyncTaskExecutor-6
Completed: Task finished 5
Completed: Task finished 0
Completed: Task finished 1
running task 8. Thread: SimpleAsyncTaskExecutor-9
running task 4. Thread: SimpleAsyncTaskExecutor-5
Completed: Task finished 8
running task 7. Thread: SimpleAsyncTaskExecutor-8
running task 9. Thread: SimpleAsyncTaskExecutor-10
Completed: Task finished 9
running task 6. Thread: SimpleAsyncTaskExecutor-7
Completed: Task finished 3
Completed: Task finished 2
Completed: Task finished 6
Completed: Task finished 7
Completed: Task finished 4
Original Post




See Also