SimpleAsyncTaskExecutor implements AsyncListenableTaskExecutor which is a sub interface of AsyncTaskExecutor
SimpleAsyncTaskExecutor
AsyncListenableTaskExecutor
AsyncTaskExecutor
This implementation of AsyncListenableExecutor creates a new Thread for each task execution.
AsyncListenableExecutor
Here's a standalone example:
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()); } }
in main thread: com.logicbig.example.TaskExecutorStandAloneExample.main()running task in thread: SimpleAsyncTaskExecutor-1
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); }; } } }
running task 0. Thread: SimpleAsyncTaskExecutor-1running task 4. Thread: SimpleAsyncTaskExecutor-5running task 3. Thread: SimpleAsyncTaskExecutor-4running task 2. Thread: SimpleAsyncTaskExecutor-3running task 1. Thread: SimpleAsyncTaskExecutor-2running task 9. Thread: SimpleAsyncTaskExecutor-10running task 8. Thread: SimpleAsyncTaskExecutor-9Task finished 0Task finished 1Task finished 2Task finished 3Task finished 4running task 7. Thread: SimpleAsyncTaskExecutor-8running task 6. Thread: SimpleAsyncTaskExecutor-7running task 5. Thread: SimpleAsyncTaskExecutor-6Task finished 5Task finished 6Task finished 7Task finished 8Task finished 9
Dependencies and Technologies Used:
Version compatibilities of spring-context with this example:
Versions in green have been tested.