首页  

SpringBoot异步任务 @Async     所属分类 springboot 浏览量 929
@EnableAsync
开启异步支持

指定线程池
@Async("taskExecutor")

AsyncAnnotationBeanPostProcessor


无返回值和有返回值的


import java.time.LocalDateTime;
import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

@Service
public class AsyncBean  {
    
    @Async
    public void doNoResult(){
        try {
            Thread.sleep(1000);
            System.out.println("AsyncBean.doNoResult done" + LocalDateTime.now());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    @Async
    public Future<String> doHasResult(){
        String str = "start="+LocalDateTime.now();
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        str = str + ",done="+LocalDateTime.now();

        return new AsyncResult<String>(str);
    }
    
}



完整代码

https://gitee.com/dyyx/springboothello/blob/master/src/main/java/demo/bean/AsyncBean.java

https://gitee.com/dyyx/springboothello/blob/master/src/main/java/demo/controller/AsyncController.java



SpringBoot定时任务 schedule

上一篇     下一篇
2020热词中英文

Java字节码增强简介

JDK CGLIB Javassist ASM 动态代理 比较

groovy实例

模板引擎velocity实例

freemarker实例