Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the volatile keyword required for fields accessed via a ReentrantLock?

My question refers to whether or not the use of a ReentrantLock guarantees visibility of a field in the same respect that the synchronized keyword provides.

For example, in the following class A, the field sharedData does not need to be declared volatile as the synchronized keyword is used.

class A  {   private double sharedData;    public synchronized void method()    {     double temp = sharedData;     temp *= 2.5;     sharedData = temp + 1;   }  } 

For next example using a ReentrantLock however, is the volatile keyword on the field necessary?

class B  {   private final ReentrantLock lock = new ReentrantLock();   private volatile double sharedData;    public void method()    {     lock.lock();     try     {       double temp = sharedData;       temp *= 2.5;       sharedData = temp + 1;     }     finally     {       lock.unlock();     }   }  } 

I know that using the volatile keyword anyway will only likely impose a miniscule performance hit, but I would still like to code correctly.

like image 984
Matthew Blackford Avatar asked Oct 15 '09 06:10

Matthew Blackford


People also ask

What is the volatile keyword useful for?

Volatile keyword is used to modify the value of a variable by different threads. It is also used to make classes thread safe. It means that multiple threads can use a method and instance of the classes at the same time without any problem.

How does ReentrantLock work in Java?

A ReentrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock.

What is the difference between synchronized keyword and ReentrantLock advantages of ReentrantLock over synchronized keyword?

In case of synchronized keyword, a thread can be blocked waiting for lock, for an indefinite period of time and there was no way to control that. ReentrantLock provides a method called lockInterruptibly(), which can be used to interrupt thread when it is waiting for lock.

What is the difference between lock and ReentrantLock?

Lock is an interface. It defines a set of methods that all locks should have. ReentrantLock is a concrete class that implements the Lock interface.


1 Answers

It's safe without volatility. ReentrantLock implements Lock, and the docs for Lock include this:

All Lock implementations must enforce the same memory synchronization semantics as provided by the built-in monitor lock, as described in The Java Language Specification, Third Edition (17.4 Memory Model):

  • A successful lock operation has the same memory synchronization effects as a successful Lock action.
  • A successful unlock operation has the same memory synchronization effects as a successful Unlock action.
like image 101
Jon Skeet Avatar answered Sep 24 '22 17:09

Jon Skeet