Object Level Lock in Java Last Updated : 01 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the code block on a given instance of the class. If a thread wants to execute a synchronized method on the given object. First, it has to get a lock of that object. Once the thread gets 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 releasing lock internally is taken care of by JVM and the programmer is not responsible for these activities. MethodsThere are different ways we can lock the object in the thread as below: Method 1: public class GeekClass{ public synchronized void GeekMethod(){}}Method 2: public class GeekClass{ public void GeekMethod(){ synchronized (this) { // other thread safe code } }}Method 3: public class DemoClass{ private final Object lock = new Object(); public void demoMethod(){ synchronized (lock) { // other thread safe code } }}Example of Object Level LockBelow is the implementation of Object Level Lock is mentioned below: Java // Java program to illustrate // Object lock concept // Class // Extending Runnable interface class Geek implements Runnable { // Method of this class public void run() { Lock(); } // Synchronization of non-static methods // (object lock) as different synchronized // non-static methods are called in both threads // Then both threads need to acquire the object lock // After one is acquired, the other thread must wait // for one thread to finish the executing // before the other thread starts to execute. 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"); } } // Main driver method public static void main(String[] args) { // Creating an object of above class // in the main() method Geek g = new Geek(); // Sharing the same object across two Threads // Here, t1 takes g Thread t1 = new Thread(g); // Here, t2 takes g Thread t2 = new Thread(g); // Creating another object of above class Geek g1 = new Geek(); // Here, t3 takes g1 Thread t3 = new Thread(g1); // setname() method is used to change // name of the thread t1.setName("t1"); t2.setName("t2"); t3.setName("t3"); // start() method beginning the execution of threads // as JVM calls the run() method of thread t1.start(); t2.start(); t3.start(); } } Outputt1 t2 t3 in block t1 in block t3 in block t3 end in block t1 end in block t2 in block t2 end Comment More infoAdvertise with us Next Article Classes and Objects in Java A aksrathod07 Follow Improve Article Tags : Java Java-Multithreading Java-Class and Object Practice Tags : JavaJava-Class and Object Similar Reads Object level and Class level locks in Java 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 in 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 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 ReentrantLock in Java ReentrantLock in Java is a part of the java.util.concurrent package that helps to achieve synchronization more effectively and optimally compared to the traditional Synchronized keyword. It offers features like,TimeoutsInterruptible locksMore control over Thread SchedulingThese features make it a va 6 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 Semaphore in Java A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource. Thus, to access the resource, a thread 6 min read Like