@Async 注解的用法和示例
文章目录
背景
异步调用
@Async介绍
在Spring中启用@Async
@Slf4j
@SpringBootApplication
@ComponentScan(basePackages = {"com.kaesar.spring"})
@EnableAsync // 开启异步调用
public class Application {
public static void main(String[] args) {
log.info("spring boot开始启动...");
ApplicationContext ctx = SpringApplication.run(Application.class, args);
String[] activeProfiles = ctx.getEnvironment().getActiveProfiles();
for (String profile : activeProfiles) {
log.info("当前环境为:" + profile);
}
log.info("spring boot启动成功...");
}
}
示例一:基本使用方式
/**
* 异步方法
* 默认情况下,Spring 使用 SimpleAsyncTaskExecutor 去执行这些异步方法(此执行器没有限制线程数)。
* 此默认值可以从两个层级进行覆盖:
* 方法级别
* 应用级别
*/
@Async
public void test2() {
try {
log.info(Thread.currentThread().getName() + " in test2, before sleep.");
Thread.sleep(2000);
log.info(Thread.currentThread().getName() + " in test2, after sleep.");
} catch (InterruptedException e) {
log.error("sleep error.");
}
}
/**
* 调用不同类的异步方法
*/
public void func1() {
log.info("before call async function.");
asyncService.test2();
log.info("after call async function.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
log.error("sleep error.");
}
log.info("func end.");
}
示例二:在同一个类中调用异步方法
示例三:异步方法是static方法
/**
* 异步方法不能是 static 方法,不然注解失效
*/
@Async
public static void test3() {
try {
log.info(Thread.currentThread().getName() + " in test3, before sleep.");
Thread.sleep(2000);
log.info(Thread.currentThread().getName() + " in test3, after sleep.");
} catch (InterruptedException e) {
log.error("sleep error.");
}
}
/**
* 调用不同类的异步方法,异步方法是 static 方法
*/
public void func3() {
log.info(Thread.currentThread().getName() + ": before call async function.");
AsyncService.test3();
log.info(Thread.currentThread().getName() + ": after call async function.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
log.error("sleep error.");
}
log.info(Thread.currentThread().getName() + ": func end.");
}
示例四:在方法级别上修改默认的执行器
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* 自定义线程池
*/
@Configuration
public class AsyncConfig {
private static final int MAX_POOL_SIZE = 10;
private static final int CORE_POOL_SIZE = 5;
@Bean("asyncTaskExecutor")
public AsyncTaskExecutor asyncTaskExecutor() {
ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
asyncTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
asyncTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
asyncTaskExecutor.setThreadNamePrefix("async-task-thread-pool-");
asyncTaskExecutor.initialize();
return asyncTaskExecutor;
}
}
/**
* 在方法级别上修改默认的执行器
*/
@Async("asyncTaskExecutor")
public void test4() {
try {
log.info(Thread.currentThread().getName() + ": in test4, before sleep.");
Thread.sleep(2000);
log.info(Thread.currentThread().getName() + ": in test4, after sleep.");
} catch (InterruptedException e) {
log.error("sleep error.");
}
}
/**
* 调用不同类的异步方法
*/
public void func4() {
log.info(Thread.currentThread().getName() + ": before call async function.");
asyncService.test4();
log.info(Thread.currentThread().getName() + ": after call async function.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
log.error("sleep error.");
}
log.info(Thread.currentThread().getName() + ": func end.");
}