ReentrantLock
![](https://cdn.codegym.cc/images/article/ee2cd199-b06a-4310-93c0-67f113b8ba93/1024.jpeg)
Condition - applying conditions in locks allows you to achieve control over the management of access to streams. The lock condition is an object of the Condition interface from thejava.util.concurrent.locks. Using Condition objectsis in many ways similar to using thewait/notify/notifyAll methods of the Object class, which were discussed in one of the previous topics.
Lock is an interface fromthe lock frameworkthat provides a flexible approach to restricting access to resources/blocks compared to synchronized. When using several locks, the order of their release can be arbitrary, plus it can also be configured. There is also the possibility to handle the situation when the lock is already captured.
ReentrantLock is one of the implementations of the Lock interface, the ReentrantLock class. It allows the same thread to call the lock method, even if it has called it before, without releasing the lock.
The ReentrantLock class , in addition to the methods of the Lock interface , has a factory method newCondition() . This method returns an objectCondition, which allows you to add the current thread to the wait set of the given objectCondition.
private final Lock R_LOCK = ReentrantLock();
R_LOCK.lock();
try {
//some action happens here
} finally {
R_LOCK.unlock();
}
ReadWriteLock is an interface for creating read/write locks. Locks are extremely useful when a system has a lot of reads and few writes.
ReentrantReadWriteLock - used in multi-threaded services and caches, has a nice performance boost compared to synchronized blocks. In fact, the class works in 2 mutually exclusive modes: many readers read data in parallel and when only 1 writer writes data.
ReentrantReadWriteLock.ReadLock - read lock for readers, obtained via readWriteLock.readLock().
ReentrantReadWriteLock.WriteLock - write lock for writers, obtained via readWriteLock.writeLock().
Synchronizer
![](https://cdn.codegym.cc/images/article/cb9ae844-7dce-459b-b8af-55a1830c5fb1/512.jpeg)
AbstractOwnableSynchronizer is the base class responsible for building synchronization mechanisms. Contains a getter/setter to remember and read an exclusive stream that can operate on your data.
AbstractQueuedSynchronizer is the base class for the synchronization mechanism in FutureTask, CountDownLatch, Semaphore, ReentrantLock, ReentrantReadWriteLock. It is also used when creating new synchronization mechanisms that rely on a single and atomic int value.
AbstractQueuedLongSynchronizer is a variant of AbstractQueuedSynchronizer that supports the atomic long value.