springboot -- 方法异步设置

1、主程序添加 @EnableAsync 开启异步

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@SpringBootApplication
public class SpringbootDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootDemoApplication.class, args);
	}

}

2、在方法上添加 @Async

package com.example.service;

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

@Service
public class TestService {
	
	/*
	 * 异步
	 */
	@Async
	public void test(){
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("hello");
	}
}

发布了52 篇原创文章 · 获赞 1 · 访问量 1756

猜你喜欢

转载自blog.csdn.net/qq_42039738/article/details/104107910
今日推荐