Does making a Reentrant Lock static and make it a mutex?

后端 未结 2 1107
暗喜
暗喜 2021-02-08 04:46

In Brian Goetz\'s book, Java Concurrency in Practice, his example of a Reentrant lock is programmed like this:

Lock lock = new ReentrantLock();

相关标签:
2条回答
  • 2021-02-08 05:04

    Yes.

    final and private have no influence, of course, but static means that all instances share the same lock.

    So if you have two instances, the code block can't be executed by two threads at the same time.

    If the lock isn't static, each instance gets its own lock. That means that more threads can run the code at the same time (depending on which instance they work, of course).

    0 讨论(0)
  • 2021-02-08 05:21

    Creating a static Lock is equivallent to

    synchronized(MyClass.class){
    
    }
    

    Its in essence a class level lock

    0 讨论(0)
提交回复
热议问题