What is the relationship between spring scope and threads?

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

Feb.27,2021

personally, I think you have confused singleton with thread safety. Singleton does not mean thread safety. Multiple threads accessing singletons at the same time also has concurrency problems, unless you add locks manually. Through an example, how much do you think the count will be in the end?

@oxf1992</a> 

controller <br>controller serviceservice

@RestController
@RequestMapping("/test")
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private IUserService userService;

    @GetMapping("/user")
    public Object testUser() {
        logger.info("testController:{}", this.toString());
        logger.info("userService:{}", userService.toString());
        userService.get(1l);
        return "ok";
    }

    @RequestMapping("/foo2")
    public Object foo2(HttpServletRequest request) throws InterruptedException {
        String param = request.getParameter("p");
        logger.error("param:{}", param);
        long start = System.currentTimeMillis();
        Thread.sleep(10000);
        long end = System.currentTimeMillis();
        logger.error("time:{}", end - start);
        return end - start;
    }
}


    @Override
    public void get(Long id) {
        logger.error("get start...{}", this.toString());
        try {
            Thread.sleep(30000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.error("get end...{}", this.toString());
    }

the results are as follows:

2018-03-28 15:53:04.559  INFO 21776 --- [io-8080-exec-10] c.e.m.web.controller.TestController      : testController:com.example.multimodule.web.controller.TestController@189ebbd
2018-03-28 15:53:04.559  INFO 21776 --- [io-8080-exec-10] c.e.m.web.controller.TestController      : userService:com.example.multimodule.service.service.impl.UserServiceImpl@2a12ba65
2018-03-28 15:53:04.559 ERROR 21776 --- [io-8080-exec-10] c.e.m.s.service.impl.UserServiceImpl     : get start...com.example.multimodule.service.service.impl.UserServiceImpl@2a12ba65
2018-03-28 15:53:25.665  INFO 21776 --- [nio-8080-exec-4] c.e.m.web.controller.TestController      : testController:com.example.multimodule.web.controller.TestController@189ebbd
2018-03-28 15:53:25.665  INFO 21776 --- [nio-8080-exec-4] c.e.m.web.controller.TestController      : userService:com.example.multimodule.service.service.impl.UserServiceImpl@2a12ba65
2018-03-28 15:53:25.665 ERROR 21776 --- [nio-8080-exec-4] c.e.m.s.service.impl.UserServiceImpl     : get start...com.example.multimodule.service.service.impl.UserServiceImpl@2a12ba65
2018-03-28 15:53:34.559 ERROR 21776 --- [io-8080-exec-10] c.e.m.s.service.impl.UserServiceImpl     : get end...com.example.multimodule.service.service.impl.UserServiceImpl@2a12ba65
2018-03-28 15:53:55.665 ERROR 21776 --- [nio-8080-exec-4] c.e.m.s.service.impl.UserServiceImpl     : get end...com.example.multimodule.service.service.impl.UserServiceImpl@2a12ba65
2018-03-28 15:54:01.853 ERROR 21776 --- [nio-8080-exec-8] c.e.m.web.controller.TestController      : param:aaaaa
2018-03-28 15:54:03.788 ERROR 21776 --- [nio-8080-exec-5] c.e.m.web.controller.TestController      : param:bbbb
2018-03-28 15:54:11.854 ERROR 21776 --- [nio-8080-exec-8] c.e.m.web.controller.TestController      : time:10000
2018-03-28 15:54:13.788 ERROR 21776 --- [nio-8080-exec-5] c.e.m.web.controller.TestController      : time:10000

you can see that the foo2 request is executed twice, basically at the same time, but there is a long interval between the two execution of user,

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36497-4ec67.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36497-4ec67.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?