@MyLog("常用语列表")@ApiOperation(value = "常用语列表")@GetMapping(value = "/test/{orderId}")public void test(@PathVariable String orderId) { System.out.println(System.identityHashCode( orderId)); synchronized (orderId.intern()){ a = a+1; log.info("开始:{}",orderId); log.info("结束:{}",orderId); } System.out.println("a="+a);}public static void main(String[] args) { System.out.println(System.identityHashCode(new String("a"))); System.out.println(System.identityHashCode(new String("a"))); System.out.println(System.identityHashCode(new String("a"))); System.out.println(System.identityHashCode(new String("a"))); System.out.println(System.identityHashCode(new String("a"))); System.out.println(System.identityHashCode(new String("a")));}
Map<String, ReentrantLock> mutexCache = new HashMap<>(); @MyLog("常用语列表") @ApiOperation(value = "常用语列表") @GetMapping(value = "/test/{orderId}") public void test(@PathVariable String orderId) { ReentrantLock mutexKey = mutexCache.get(orderId); if (mutexKey == null) { mutexKey = new ReentrantLock(); mutexCache.put(orderId, mutexKey); } mutexKey.lock(); log.info("开始:{}", orderId); log.info("结束:{}", orderId); mutexKey.unlock(); mutexCache.remove(orderId); System.out.println(mutexCache.size()); }