Spring MVC的同步請求模式

瀏覽器或APP發起請求,web服務器創建線程處理請求,處理完請求並把處理結果返回給客戶端。在獲取返回結果前web服務器出於阻塞狀態,客戶端一直在等待。

Spring MVC的同步請求模式

同步請求流程

<code> @RequestMapping("/sync")
@ResponseBody
public String syncReq(@RequestParam String taskId){
System.out.println("main thread:"+Thread.currentThread().getName());
System.out.println("begin execute task"+System.currentTimeMillis());
String response = null;
try {
response = executeTask(taskId);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end execute task"+System.currentTimeMillis());
return response;
}

private String executeTask(String param) throws InterruptedException {
System.out.println("exec thread:"+Thread.currentThread().getName());
System.out.println("execute task:"+param);
Thread.sleep(5000);
return "sucess";
}/<code>

執行結果:執行任務的線程id相同

<code>main thread:http-apr-8080-exec-8
begin execute task1585984865968
exec thread:http-apr-8080-exec-8
execute task:111
end execute task1585984870968/<code>


分享到:


相關文章: