标签@EnableAsync和@Async实现异步操作

标签@EnableAsync和@Async

通过@EnableAsync开启对异步任务的支持,并通过在实际执行的Bean的方法中使用@Async注解来声明其是一个异步任务,通过Spring提供的ThreadPoolTaskExecutor使用线程池

 

首先定义配置类

package com.hy.spring.test7;  
  
import java.util.concurrent.Executor;  
  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.scheduling.annotation.EnableAsync;  
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;  
  
@Configuration  
@ComponentScan("com.hy.spring.test7")  
@EnableAsync  // 启用异步任务  
public class ThreadConfig  {  
  
     // 执行需要依赖线程池,这里就来配置一个线程池  
     @Bean  
     public Executor getExecutor() {  
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  
          executor.setCorePoolSize(5);  
          executor.setMaxPoolSize(10);  
          executor.setQueueCapacity(25);  
          executor.initialize();  
          return executor;  
     }  
}  

定义要执行的任务:

package com.hy.spring.test7;  
  
import java.util.Random;  
import java.util.UUID;  
  
import org.springframework.scheduling.annotation.Async;  
import org.springframework.stereotype.Service;  
  
@Service  
public class AsynTaskService {  
  
     @Async    // 这里进行标注为异步任务,在执行此方法的时候,会单独开启线程来执行  
     public void f1() {  
          System.out.println("f1 : " + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString());  
          try {  
              Thread.sleep(new Random().nextInt(100));  
          } catch (InterruptedException e) {  
              e.printStackTrace();  
          }  
     }  
  
     @Async  
     public void f2() {  
          System.out.println("f2 : " + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString());  
          try {  
              Thread.sleep(100);  
          } catch (InterruptedException e) {  
              e.printStackTrace();  
          }  
     }  
}  

测试类:

package com.hy.spring.test7;  
  
import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  
public class Main {  
  
     public static void main(String[] args) {  
          AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ThreadConfig.class);  
          AsynTaskService service = context.getBean(AsynTaskService.class);  
  
          for (int i = 0; i < 10; i++) {  
              service.f1(); // 执行异步任务  
              service.f2();  
          }  
          context.close();  
     }  
}  

输出结果
 

f1 : ThreadPoolTaskExecutor-5   20e6ba88-ae51-42b9-aac6-ed399419fe6d

f2 : ThreadPoolTaskExecutor-2   0d7b1da4-e045-4d58-9054-e793f931cae1

f2 : ThreadPoolTaskExecutor-4   17b8d7c7-24e3-4bcf-b4da-822650a8f0be

f1 : ThreadPoolTaskExecutor-3   a9b32322-1c9b-4fc7-9c2a-1f7a81f2b089

f1 : ThreadPoolTaskExecutor-1   13a85fde-73c7-4c9b-9bb2-92405d1d3ac4

f2 : ThreadPoolTaskExecutor-3   8896caaf-381c-4fc3-ab0f-a42fcc25e5fd

f1 : ThreadPoolTaskExecutor-5   48246589-f8e9-4e9c-b017-8586bf14c0b0

f2 : ThreadPoolTaskExecutor-1   291b03ea-154f-46ba-bc41-69a61d1dd4d5

f1 : ThreadPoolTaskExecutor-4   856d8f48-70b4-475a-80cc-27d1635be36b

f2 : ThreadPoolTaskExecutor-2   1f7b1918-cf10-49a3-aaec-7b97a3a67e7d

f1 : ThreadPoolTaskExecutor-3   12e56d3f-d042-42dd-a387-3de80201c3b2

f2 : ThreadPoolTaskExecutor-5   bf0dbc97-61d8-4644-9ae4-4d711228198d

f1 : ThreadPoolTaskExecutor-3   4c58793a-394e-4241-87e6-fff1f480518d

f2 : ThreadPoolTaskExecutor-4   fa1d4484-d3a4-4303-9ffe-b6aaa791b157

f1 : ThreadPoolTaskExecutor-1   67144d54-d158-4c6a-865e-b80668515bea

f2 : ThreadPoolTaskExecutor-2   c48cfa18-48d4-4778-8f09-04b779338816

f1 : ThreadPoolTaskExecutor-3   30143849-3c49-4128-a811-f6468a091114

f2 : ThreadPoolTaskExecutor-5   58603271-ee4e-40c9-b6ff-199d32dfb02a

f1 : ThreadPoolTaskExecutor-1   3b0ce7ff-fdff-4e23-bb44-d1a0c1148982

f2 : ThreadPoolTaskExecutor-3   cb9a1543-955a-4bc9-b4e9-6b61188371ee

可以看到两个任务是异步进行的。

下面关于线程池的配置还有一种方式,就是直接实现AsyncConfigurer接口,重写getAsyncExecutor方法即可,代码如下

package com.hy.spring.test7;  
  
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("com.hy.spring.test7")  
@EnableAsync  
public class ThreadConfig implements AsyncConfigurer {  
  
     @Override  
     public Executor getAsyncExecutor() {  
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  
          executor.setCorePoolSize(5);  
          executor.setMaxPoolSize(10);  
          executor.setQueueCapacity(25);  
          executor.initialize();  
          return executor;  
     }  
  
     @Override  
     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {  
          return null;  
     }  
  
}  

 

你可能感兴趣的:(spring)