Tuesday, December 31, 2024
Google search engine
HomeLanguagesJavaObject Level Lock in Java

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. 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 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 by JVM and the programmer is not responsible for these activities

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 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.

Methods: There 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:

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();
    }
}


 
 

Output

t1
t3
t2
in block t1
in block t3
in block t1 end
in block t2
in block t2 end
in block t3 end

 

RELATED ARTICLES

Most Popular

Recent Comments