A dot eyeball.

Spring implements multithreading and concurrent programming through the TaskExecutor. Use ThreadPoolTaskExecutor to implement a TaskExecutor based on a thread pool. In real development, tasks are usually non-blocking, i.e. asynchronous, so we will enable support for asynchronous tasks in the configuration class via @enableAsync, and declare it as an asynchronous task by using the @Async annotation in the method of the actual executing Bean.

Example 2.

1. The configuration class

package org.light4j.sping4.senior.taskExecutor; import java.util.concurrent.Executor; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @ Configuration @ ComponentScan (" org. Light4j. Sping4. Pro. TaskExecutor ") @ EnableAsync / / (1) the public class TaskExecutorConfig Implements AsyncConfigurer{//② @override public Executor getAsyncExecutor() {//② ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(10); taskExecutor.setQueueCapacity(25); taskExecutor.initialize(); return taskExecutor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; }}Copy the code

Code explanation:

① Enable asynchronous task support with the @enableAsync annotation. The AsyncConfigurer interface overwrites the getAsyncExecutor method and returns ThreadPoolTaskExecutor.

2. Task execution classes

package org.light4j.sping4.senior.taskExecutor; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @service public class AsyncTaskService {@async //① public void executeAsyncTask(Integer I){system.out.println ("  "+i); } @async public void executeAsyncTaskPlus(Integer I){system.out.println (" Async task +1: "+(I +1));} @async public void executeAsyncTaskPlus(Integer I){system.out.println (" Async task +1: "+(I +1)); }}Copy the code

Code explanation:

The @async annotation indicates that the method is an asynchronous method. If the annotation is at the class level, it indicates that all methods in the class are asynchronous methods, which are automatically injected using ThreadPoolTaskExecutor as TaskExecutor.

3. Run

package org.light4j.sping4.senior.taskExecutor; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class); AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class); for(int i =0 ; i<10; i++){ asyncTaskService.executeAsyncTask(i); asyncTaskService.executeAsyncTaskPlus(i); } context.close(); }}Copy the code

The output shows that it is executed concurrently rather than sequentially,

The running results are as follows:

4. Source code examples:

Github Address: Click to view code cloud address: click to view

exceptional
Welcome to follow the life designer’s wechat public account



longjiazuoA