在方法上添加@Async注解,在启动类添加@EnableAsync注解
controller
@RequestMapping("sync")
public void fun1() {
System.out.println("1");
service.fun1();
System.out.println("4");
}
service
@Async
public void fun1() {
// TODO Auto-generated method stub
System.out.println("2");
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("#######"+i);
};
System.out.println("3");
}
不加的话是正常顺序1234,
加了是主线程先执行完1423,也可能是1243吧