前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Choosing the object-to-lock in explicit locks (ReentrantLock example)

Choosing the object-to-lock in explicit locks (ReentrantLock example)

作者头像
一个会写诗的程序员
发布2019-07-08 00:50:43
4120
发布2019-07-08 00:50:43
举报
文章被收录于专栏:一个会写诗的程序员的博客

Choosing the object-to-lock in explicit locks (ReentrantLock example)

https://coderanch.com/t/665262/java/Choosing-object-lock-explicit-locks

So, I just finished studying synchronized keyword (Blocks and methods). And here is what i basically know.

A synchronized keyword guarantees atomicity and visibility and it has to operate on two things. 1- A thread. (The calling thread) 2- An object. (The monitor object)

When using

代码语言:javascript
复制
synchronized (object) {
    //do stuff
}

The object "object" will be locked on the calling thread only. until the lock is released.

So, When i started attempting to study explicit locks, I came across this example [Source] Java 8 Concurrency Tutorial: Synchronization and Locks - Part 2: Synchronization and Locks https://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/

ReentrantLock

The class ReentrantLock is a mutual exclusion lock with the same basic behavior as the implicit monitors accessed via the synchronized keyword but with extended capabilities. As the name suggests this lock implements reentrant characteristics just as implicit monitors. Let's see how the above sample looks like using ReentrantLock

代码语言:javascript
复制
ReentrantLock lock = new ReentrantLock();
int count = 0;
 
void increment() {
    lock.lock();
    try {
        count++;
    } finally {
        lock.unlock();
    }
}

Which brings me to my questions:

1- Is what's previously mentioned correct and generally accurate? 2- When using explicit locks (in the above ReentrantLock example), How do i get to choose the object that i want locked? Assuming that the calling thread is the thread that will have the lock.

You decide the policy for how and which objects to lock.

In general, you create a lock for each set of operations that should not be allowed to run concurrently.

Let's say you have an object that has two completely unrelated fields:

代码语言:javascript
复制
final class MyObject {
 
  private final Thing a = Thing.something();
  private final Thing b = Thing.something();
 
  private final Lock aLock = new ReentrantLock();
  private final Lock bLock = new ReentrantLock();
 
  int readFromA() {
    aLock.lock();
    try { return a.read(); }
    finally { aLock.unlock(); }
  }
 
  void writeToA(int val) {
    aLock.lock();
    try { a.write(val); }
    finally { aLock.unlock(); }
  }
 
  int readFromB() {
    bLock.lock();
    try { return b.read(); }
    finally { bLock.unlock(); }
  }
 
  void writeB(int val) {
    bLock.lock();
    try { b.write(val); }
    finally { bLock.unlock(); }
  }
}

Since the two fields are unrelated and the validity of the object doesn't depend on certain combinations of a and b, one thread should be allowed to interact with a while another interacts with b. To facilitate this, you need two different locks.

A big advantage of using explicit locks over implicit ones, is that a malicious class can't lock an object indefinitely while it goes off and does other things. For instance, if readFromA() used synchronized(this), any class that has access to an instance of MyObject can use a synchronized block to lock the instance, and then keep it locked forever. With explicit locks this doesn't happen, as long as you keep locks private.

Question. What "atomicity and visibility" guarantees are you referring to? Method calls? Access to instance variables?

Simply, there are no method or variable guarantees, with owning a synchronization lock on an object. The only guarantee is that only one thread can own the lock (on the object) at a time. Synchronization locks are cooperative. It is the developer's responsibility to take the "only one thread can own the lock" guarantee, and make that into visibility and atomicity guarantees.

Henry

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.07.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Choosing the object-to-lock in explicit locks (ReentrantLock example)
    • ReentrantLock
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档