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

后端 未结 2 1108
暗喜
暗喜 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).

提交回复
热议问题