1. @MyLog("常用语列表")
    2. @ApiOperation(value = "常用语列表")
    3. @GetMapping(value = "/test/{orderId}")
    4. public void test(@PathVariable String orderId) {
    5. System.out.println(System.identityHashCode( orderId));
    6. synchronized (orderId.intern()){
    7. a = a+1;
    8. log.info("开始:{}",orderId);
    9. log.info("结束:{}",orderId);
    10. }
    11. System.out.println("a="+a);
    12. }
    13. public static void main(String[] args) {
    14. System.out.println(System.identityHashCode(new String("a")));
    15. System.out.println(System.identityHashCode(new String("a")));
    16. System.out.println(System.identityHashCode(new String("a")));
    17. System.out.println(System.identityHashCode(new String("a")));
    18. System.out.println(System.identityHashCode(new String("a")));
    19. System.out.println(System.identityHashCode(new String("a")));
    20. }
    1. Map<String, ReentrantLock> mutexCache = new HashMap<>();
    2. @MyLog("常用语列表")
    3. @ApiOperation(value = "常用语列表")
    4. @GetMapping(value = "/test/{orderId}")
    5. public void test(@PathVariable String orderId) {
    6. ReentrantLock mutexKey = mutexCache.get(orderId);
    7. if (mutexKey == null) {
    8. mutexKey = new ReentrantLock();
    9. mutexCache.put(orderId, mutexKey);
    10. }
    11. mutexKey.lock();
    12. log.info("开始:{}", orderId);
    13. log.info("结束:{}", orderId);
    14. mutexKey.unlock();
    15. mutexCache.remove(orderId);
    16. System.out.println(mutexCache.size());
    17. }