@Slf4j
@RestController
public class DemoController {
@GetMapping
public String testStr(String str) {
String key = UUID.randomUUID().toString();
log.info("testStr start {} {}", str, key);
synchronized (str.intern()) {
log.info("testStr in {} {}", str, key);
Thread.sleep(5000);
}
log.info("testStr end {} {}", str, key);
return str;
}
}
当我有两个要求的时候 ?str=aaa
,日志如下所示:
testStr start aaa 3bbb03e3-84d9-4816-b275-de59657bd810
testStr in aaa 3bbb03e3-84d9-4816-b275-de59657bd810
... after 5 sec
testStr end aaa 3bbb03e3-84d9-4816-b275-de59657bd810
testStr start aaa b4f07a65-e37e-4471-be79-877a8d529f04
testStr in aaa b4f07a65-e37e-4471-be79-877a8d529f04
... after 5 sec
testStr end aaa b4f07a65-e37e-4471-be79-877a8d529f04
但我觉得应该是
testStr start aaa 3bbb03e3-84d9-4816-b275-de59657bd810
testStr in aaa 3bbb03e3-84d9-4816-b275-de59657bd810
testStr start aaa b4f07a65-e37e-4471-be79-877a8d529f04
... after 5sec
testStr in aaa b4f07a65-e37e-4471-be79-877a8d529f04
testStr end aaa 3bbb03e3-84d9-4816-b275-de59657bd810
... after 5 sec
testStr end aaa b4f07a65-e37e-4471-be79-877a8d529f04
不同的部分 testStr start...
应在5秒前记录。
我错过什么了吗?
顺便说一句,我用简单的main测试这些代码,没有spring的东西,然后一切都按预期工作。
主代码
public static void main(String[] args) {
DemoController demoController = new DemoController();
new Thread(()->{
String result = demoController.testStr("aaa");
log.info("result1 {}", result);
}).start();
new Thread(()->{
String result = demoController.testStr("aaa");
log.info("result2 {}", result);
}).start();
}
暂无答案!
目前还没有任何答案,快来回答吧!