Home » Core Java » util » concurrent » locks

locks

Java ReentrantReadWriteLock Example

This is an example of how to make use of the ReentrantReadWriteLock class of Java. It is an implementation of ReadWriteLock, that also supports ReentrantLock functionality. The ReadWriteLock is a pair of associated Locks, one for read-only operations and one for writing. Whereas, the ReentrantLock is a reentrant mutual exclusion Lock with the same behavior as the implicit monitor lock accessed using synchronized methods ...

Read More »

java.util.concurrent.locks.AbstractQueuedSynchronizer Example

AbstractQueuedSynchronizer class has support for exclusive and shared mode of locks and synchronizers. It provides methods for inspection, instrumentation and monitoring methods for condition objects. To implement, the following methods for releasing, acquiring shared, releasing shared, exclusively holding, getting state, setting state and comparing state have to be reimplemented:           tryAcquire(int) tryRelease(int) tryAcquireShared(int) tryReleaseShared(int) isHeldExclusively() getState() ...

Read More »

Java ReadWriteLock Example

In this example, we are going to demonstrate the use of ReadWriteLock in Java. Mutually Exclusive Locks, as the ReentrantLock discussed in the previous article, offer far less level of concurrency than non-mutually exclusive locks. If multiple threads are accessing an object for reading data, it does not make sense to use a synchronized block or any other mutually exclusive ...

Read More »

java.util.concurrent.locks.Condition Example

In this tutorial we will discuss about the Condition interface in Java. A Condition object, also known as condition variable, provides a thread with the ability to suspend its execution, until the condition is true. A Condition object is necessarily bound to a Lock and can be obtained using the newCondition() method. Furthermore, a Condition enables the effect of having ...

Read More »

Java ReentrantLock Example

In this example, we shall be demonstrating how to use ReentrantLock in Java. Rigidness of Intrinsic Locking : The traditional way of providing synchronization in multi-threaded environment was to use the synchronized keyword. However, the synchronized keyword is considered rather rigid under certain circumstances.For example, if a thread is already executing inside a synchronized block and another thread tries to enter the ...

Read More »