springmvc异步线程执行.md
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.WebAsyncTask;
import java.util.concurrent.Callable;
@RestController
@RequestMapping
public class TestAnycController {
@GetMapping("/testAsync")
public WebAsyncTask<String> webAsyncTask(){
System.out.println("testAsync......before");
WebAsyncTask<String> stringWebAsyncTask = new WebAsyncTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(2000);
int i = 1/0;
return "异步执行代码执行返回";
}
});
System.out.println("testAsync......after");
return stringWebAsyncTask;
}
}