java的synchronized关键字是堆某对象加锁,但是我们当需要对某个字符串加锁怎么办

比如对同一个订单只能有一个操作,但是对其他订单的操作不影响

 

使用 guava包下的  Interner 类

 private static final Interner<String> pool = Interners.newWeakInterner();

synchronized ( pool.intern("Order_"+orderId)){

//TODO:something

}

 

相关文章:

  • 2021-12-22
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
猜你喜欢
  • 2022-01-22
  • 2022-01-07
  • 2021-07-01
  • 2022-01-18
  • 2022-12-23
相关资源
相似解决方案