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 8. Thread: SimpleAsyncTaskExecutor-9
running task 9. Thread: SimpleAsyncTaskExecutor-10
Task finished 0
Task finished 1
Task finished 2
Task finished 3
Task finished 4
running task 5. Thread: SimpleAsyncTaskExecutor-6
running task 7. Thread: SimpleAsyncTaskExecutor-8
running task 6. Thread: SimpleAsyncTaskExecutor-7
Task finished 5
Task finished 6
Task finished 7
Task finished 8
Task finished 9
Original Post




See Also