spring控制器似乎是块整个方法,而不是代码块

cotxawn7  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(320)
  1. @Slf4j
  2. @RestController
  3. public class DemoController {
  4. @GetMapping
  5. public String testStr(String str) {
  6. String key = UUID.randomUUID().toString();
  7. log.info("testStr start {} {}", str, key);
  8. synchronized (str.intern()) {
  9. log.info("testStr in {} {}", str, key);
  10. Thread.sleep(5000);
  11. }
  12. log.info("testStr end {} {}", str, key);
  13. return str;
  14. }
  15. }

当我有两个要求的时候 ?str=aaa ,日志如下所示:

  1. testStr start aaa 3bbb03e3-84d9-4816-b275-de59657bd810
  2. testStr in aaa 3bbb03e3-84d9-4816-b275-de59657bd810
  3. ... after 5 sec
  4. testStr end aaa 3bbb03e3-84d9-4816-b275-de59657bd810
  5. testStr start aaa b4f07a65-e37e-4471-be79-877a8d529f04
  6. testStr in aaa b4f07a65-e37e-4471-be79-877a8d529f04
  7. ... after 5 sec
  8. testStr end aaa b4f07a65-e37e-4471-be79-877a8d529f04

但我觉得应该是

  1. testStr start aaa 3bbb03e3-84d9-4816-b275-de59657bd810
  2. testStr in aaa 3bbb03e3-84d9-4816-b275-de59657bd810
  3. testStr start aaa b4f07a65-e37e-4471-be79-877a8d529f04
  4. ... after 5sec
  5. testStr in aaa b4f07a65-e37e-4471-be79-877a8d529f04
  6. testStr end aaa 3bbb03e3-84d9-4816-b275-de59657bd810
  7. ... after 5 sec
  8. testStr end aaa b4f07a65-e37e-4471-be79-877a8d529f04

不同的部分 testStr start... 应在5秒前记录。
我错过什么了吗?
顺便说一句,我用简单的main测试这些代码,没有spring的东西,然后一切都按预期工作。
主代码

  1. public static void main(String[] args) {
  2. DemoController demoController = new DemoController();
  3. new Thread(()->{
  4. String result = demoController.testStr("aaa");
  5. log.info("result1 {}", result);
  6. }).start();
  7. new Thread(()->{
  8. String result = demoController.testStr("aaa");
  9. log.info("result2 {}", result);
  10. }).start();
  11. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题