SpringBoot(异步方法)

1.异步方法:

方法一:使用线程
    @GetMapping("/test")
    public void getTest() throws Exception {
    	//开始的方法
        System.out.println("开始");
        //使用线程实现异步执行---------------------------
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("耗时操作--异步执行之方法");
                }
            }
        }).start();
		//结束的方法
        System.out.println("结束");
    }

方法二:注解
@SpringBootApplication
@EnableAsync//在启动类上开启异步注解功能

@Async//在异步方法上使用 '@Async' 注解 , 注意:该方法必须可重写
 public  void method(){
	//方法体......
}

你可能感兴趣的:(后端_SpringBoot,spring,boot)