同步程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回。异步程序在顺序执行时,不等待异步调用的语句返回结果就执行后面的程序,当一个异步过程调用发出后,调用者不能立刻得到结果。同步代码service层:public void test() throws InterruptedException { Thread.sleep(2000); for (int i = 0; i < 1000; i++) { System.out.println(“i = " + i); } }controller层: @GetMapping(“test”) public String test() { try { Thread.sleep(1000); System.out.println(“主线程开始”); for (int j = 0; j < 100; j++) { System.out.println(“j = " + j); } asyncService.test(); System.out.println(“主线程结束”); return “async”; } catch (InterruptedException e) { e.printStackTrace(); return “fail”; } }浏览器中请求 http://localhost:8080/test 控制台打印顺序:主线程开始打印j循环打印i循环主线程结束异步代码在service层的test方法上加上@Async注解,同时为了是异步生效在启动类上加上@EnableAsync注解 service层: @Async public void test() throws InterruptedException { Thread.sleep(2000); for (int i = 0; i < 1000; i++) { System.out.println(“i = " + i); } }controller不变,启动类:@SpringBootApplication@EnableAsyncpublic class AsyncApplication { public static void main(String[] args) { SpringApplication.run(AsyncApplication.class, args); }}再次请求打印顺序如下:主线程开始打印j循环主线程结束打印i循环