TestController内写:
@AutowiredComprehensiveAnalysisController comprehensiveAnalysisController;@GetMapping("async")publicStringgetAsyncInfo(Map map){System.out.println("--------------------");
comprehensiveAnalysisController.asyncInfo();String str=Thread.currentThread().getName();System.out.println(str);return"外部线程名:"+ str;}
在另一个类中写:
@AsyncpublicvoidasyncInfo(){try{Thread.sleep(9000);}catch(InterruptedException e){
e.printStackTrace();}// System.out.println("----" + Thread.currentThread().getName() +"----");System.out.println("----"+"异步执行名:"+"----"+Thread.currentThread().getName());}
总结: 被Async标记的方法, 会以异步的方式进行。
但是要注意: 调用被async标记的方法时,必须是不同的类进行调用,不能是同一个类中调用。否则不起作用。
如果A类中有被async标记的方法,则只能通过B,C等类调用,不能用A类调用。