浏览器或APP发起请求,web服务器创建线程处理请求,处理完请求并把处理结果返回给客户端。在获取返回结果前web服务器出于阻塞状态,客户端一直在等待。
<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>
閱讀更多 非典型程序猿平頭哥 的文章