Spring Boot 异步任务

1.@EnableAsync  注解主要是为了扫描范围包下的所有 @Async注解

@SpringBootApplication
@EnableAsync
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2.@Async 开启异步任务

@Component
public class SyncTask {

    @Async
    public String getTest1() {
        try {
            Thread.sleep(5000);
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
        System.err.println("执行异步任务");
        return Thread.currentThread().getName() + "执行完毕";
    }
}

 

你可能感兴趣的:(Java)