Object level and Class level locks in Java
Last Updated :
03 Apr, 2024
Synchronization:
Synchronization is a modifier that is used for the method and blocks only. With the help of a synchronized modifier, we can restrict a shared resource to be accessed only by one thread. When two or more threads need access to shared resources, there is some loss of data i.e. data inconsistency. The process by which we can achieve data consistency between multiple threads is called Synchronization.
Why do you need Synchronization?
Let us assume if you have two threads that are reading and writing to the same 'resource'. Suppose there is a variable named geek, and you want that at one time only one thread should access the variable(atomic way). But Without the synchronized keyword, your thread 1 may not see the changes thread 2 made to geek, or worse, it may only be half changed that cause the data inconsistency problem. This would not be what you logically expect. The tool needed to prevent these errors is synchronization.
In synchronization, there are two types of locks on threads:
- Object-level lock: Every object in java has a unique lock. Whenever we are using a synchronized keyword, then only the lock concept will come into the picture. If a thread wants to execute then synchronized method on the given object. First, it has to get a lock-in that object. Once the thread got the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and release lock internally is taken care of by JVM and the programmer is not responsible for these activities. Let's have a look at the below program to understand the object level lock:
JAVA
// Java program to illustrate
// Object lock concept
class Geek implements Runnable {
public void run() { Lock(); }
public void Lock()
{
System.out.println(
Thread.currentThread().getName());
synchronized (this)
{
System.out.println(
"in block "
+ Thread.currentThread().getName());
System.out.println(
"in block "
+ Thread.currentThread().getName()
+ " end");
}
}
public static void main(String[] args)
{
Geek g = new Geek();
Thread t1 = new Thread(g);
Thread t2 = new Thread(g);
Geek g1 = new Geek();
Thread t3 = new Thread(g1);
t1.setName("t1");
t2.setName("t2");
t3.setName("t3");
t1.start();
t2.start();
t3.start();
}
}
Outputt1
t3
t2
in block t3
in block t1
in block t3 end
in block t1 end
in block t2
in block t2 end
- Class level lock: Every class in Java has a unique lock which is nothing but a class level lock. If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Once a thread got the class level lock, then it is allowed to execute any static synchronized method of that class. Once method execution completes automatically thread releases the lock. Let's look at the below program for better understanding:
JAVA
// Java program to illustrate class level lock
class Geek implements Runnable {
public void run() { Lock(); }
public void Lock()
{
System.out.println(
Thread.currentThread().getName());
synchronized (Geek.class)
{
System.out.println(
"in block "
+ Thread.currentThread().getName());
System.out.println(
"in block "
+ Thread.currentThread().getName()
+ " end");
}
}
public static void main(String[] args)
{
Geek g1 = new Geek();
Thread t1 = new Thread(g1);
Thread t2 = new Thread(g1);
Geek g2 = new Geek();
Thread t3 = new Thread(g2);
t1.setName("t1");
t2.setName("t2");
t3.setName("t3");
t1.start();
t2.start();
t3.start();
}
}
Outputt1
t2
t3
in block t1
in block t1 end
in block t3
in block t3 end
in block t2
in block t2 end
Reference: https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html
Similar Reads
Object Level Lock in Java Every object in Java has a unique lock. Whenever we are using a synchronized keyword, then only the lock concept will come into the picture. An object-level lock is a mechanism when we want to synchronize a non-static method or non-static code block such that only one thread will be able to execute
3 min read
Class Level Lock in Java Every class in Java has a unique lock which is nothing but class level lock. Â If a thread wants to execute a static synchronized method, then the thread requires a class level lock. Class level lock prevents multiple threads to enter a synchronized block in any of all available instances of the clas
5 min read
Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
11 min read
Object Class in Java Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
7 min read
Understanding Classes and Objects in Java The term Object-Oriented explains the concept of organizing the software as a combination of different types of objects that incorporate both data and behavior. Hence, Object-oriented programming(OOPs) is a programming model, that simplifies software development and maintenance by providing some rul
10 min read
ReentrantReadWriteLock Class in Java ReentrantReadWriteLock class of Java 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 re-entrant mutual exclusion Lock with the sam
5 min read