Sitemap
Javarevisited

A humble place to learn Java and Programming better.

Member-only story

What are Intrinsic Lock, ReentrantLock and ReadWriteLock in Java?

--

Java provides various locking mechanisms to ensure thread safety and maintain data integrity. Monitor Locks, Reentrant Locks, and Read-Write Locks are fundamental locks in Java.

My articles are free for everyone. Non-members can read this full article for free HERE.

But First, we need to understand why we require locks.

Without having proper synchronization, we can have various unexpected cases like:

  • Race Conditions: Occurs when an application’s behaviour depends on the relative timing of events between threads.
  • Data Inconsistency: This can be due to threads reading partially updated or stale data.
  • Deadlocks: This happens when two or more threads are blocked, waiting for others to release a resource
  • LiveLocks: This is when threads change their state without making real progress.

Intrinsic Locks

Intrinsic Locks, also called monitor locks, are the most common form of lock-in Java. We achieve this lock using the synchronized keyword.

Synchronized Methods

public class Counter {
private int count = 0;

public synchronized void increment() {…
Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Saquib Aftab
Saquib Aftab

Written by Saquib Aftab

Lead Software Engineer | Java | Technology | Productivity | Learning and Improving to become a better developer

Responses (1)