Having a simple List:
private final List<Item> lst = new ArrayList();
When I need synchronization, I used to do the following:
synchronized (lst) {
// Some code
}
Now, I need to create a Condition
object using Lock.newCondition()
, can I access the underlying lock used by synchronized{}
to use it to create my condition, or should I remove synchronized blocks and use a custom Lock
object ?
If you need a Condition object, is a clear sign that intrinsic locking are not enough for you. You will need to use
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
Anyway, if you are worried about performances, that is not a problem with modern virtual machines. Explicit locking perform as well as intrinsic one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.