Open In App

How to Check if a Thread Holds Lock on a Particular Object in Java?

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Java language is one of the most popular languages for many years. One of the most advantageous features of java programming is Multithreading. Multithreading allows executing a single program into many small parts of the program with the maximum utilization of the CPU. Threads are a lightweight process that helps to execute the concurrent large process efficiently. but sometimes these threads decrease the efficiency of the program. 

Let’s take a real-life example of why invalid use of the multithreading concept leads to a decrease in the efficiency and the resources of the program.

Real-life Example 

Consider a class having students and a teacher is teaching them to consider a teacher a thread because the exams of the students are coming very nearly all the different teachers(Mutlthreads) try to teach as can they possible so they are teaching randomly one by one to complete the syllabus as soon as possible. 

This might reduce the duty of the teachers i.e threads to complete their task but the burden on the students will be increased too much. To tackle this situation we have to synchronize the timing of the teachers i.e threads so that confusion between the students can be decreased. Similarly, in java we can synchronize the threads for accessing the particular resources (functions) means locking the resources so that only one resource can access them at one time. In this article, we will discuss how to check whether the thread holds a lock on a particular object in java or not.

holdLock() method is used to check the lock on the particular object during the execution time of the java program

Parameters: This method takes the reference/object of the class in which we want to check whether the lock is present on the threads or not.

Return Type: This method returns boolean parameters i.e true or false.True means monitor lock available on the current object.

Example

Java




// Java Program Illustrating How to Check if a Thread Holds
// Lock on a Particular Object
 
// Importing the libraries
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Creating the constructor
    GFG()
    {
 
        // Checking the status of the lock on the object
        System.out.println(Thread.holdsLock(this));
 
        // Synchronizing the thread
        synchronized (this)
        {
 
            // Checking the status of the lock on the object
            // using holdsLock() method over
            // same thread using this keyword
            System.out.println(Thread.holdsLock(this));
        }
    }
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a Thread class object
        Thread ob = new Thread() {
            // run() method for this thread which
            // is invoked as start() method is invoked
            public void run()
            {
 
                // Creating a class object
                // inside the run() methodof the class
                GFG ob1 = new GFG();
            }
        };
 
        // Starting the thread with
        // the help of start() method
        ob.start();
    }
}


 
 

Output

false
true

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads