how do you explain the relationship between spring scope and threads? Is there any relevance?
the problem is as follows:
spring scope is simulated in singleton mode, so it is considered that multiple requests for the same address will be executed sequentially. If the test method of an is executed five times at the same time, it will be executed sequentially
the following code:
@GetMapping("test1")
public Object test1(HttpServletRequest request) throws InterruptedException {
logger.error("this:{},in time:{}", this.toString(), System.currentTimeMillis());
String sleepFlag = request.getParameter("sleep");
if ("on".equalsIgnoreCase(sleepFlag)) {
Thread.currentThread().sleep(10000l);
logger.error("return time:{}", System.currentTimeMillis());
return "sleep 10s";
}
logger.error("return time:{}", System.currentTimeMillis());
return "no sleep";
}
1. http://localhost:8080/test1?sleep=on
2. http://localhost:8080/test1
each is executed twice in the order of 1-1, 1-2, 2-1, 2-2. The printed result is as follows:
2018-03-26 16:56:03.793 ERROR 16292 --- [nio-8080-exec-1] c.e.m.web.controller.UserController : this:com.example.multimodule.web.controller.UserController@7f74f276,in time:1522054563793
2018-03-26 16:56:07.349 ERROR 16292 --- [nio-8080-exec-2] c.e.m.web.controller.UserController : this:com.example.multimodule.web.controller.UserController@7f74f276,in time:1522054567349
2018-03-26 16:56:07.349 ERROR 16292 --- [nio-8080-exec-2] c.e.m.web.controller.UserController : return time:1522054567349
2018-03-26 16:56:09.438 ERROR 16292 --- [nio-8080-exec-4] c.e.m.web.controller.UserController : this:com.example.multimodule.web.controller.UserController@7f74f276,in time:1522054569437
2018-03-26 16:56:09.438 ERROR 16292 --- [nio-8080-exec-4] c.e.m.web.controller.UserController : return time:1522054569438
2018-03-26 16:56:13.797 ERROR 16292 --- [nio-8080-exec-1] c.e.m.web.controller.UserController : return time:1522054573797
2018-03-26 16:56:13.801 ERROR 16292 --- [nio-8080-exec-8] c.e.m.web.controller.UserController : this:com.example.multimodule.web.controller.UserController@7f74f276,in time:1522054573801
2018-03-26 16:56:23.801 ERROR 16292 --- [nio-8080-exec-8] c.e.m.web.controller.UserController : return time:1522054583801
result: during execution, 1-1 and 1-2 accord with the expected results, that is, the execution is dormant for 10 seconds after the completion of the first execution, but there is no hibernation process between 1-1 and 2-1 (there is a time difference due to manual execution), and the execution result is the same regardless of whether controller is set to scope=prototype,.
ask for an explanation