Friday, March 3, 2017

How to check if a thread holds lock on a particular object in Java

Think about a scenario where you would have to find at the run time that whether a Java thread has a lock on a particular object e.g. find out whether thread NewsReader has a lock on NewsPaper object or not ? If this questions came in any core java interview then I would automatically assume that there could be at least two answers one is hard earned raw answer which programmer would like to figure out based on fundamentals and other could be some rarely used API calls which are available in Java , by the way, this is actually asked to me in an interview of one of the biggest global Investment bank. Investment banking world, particularly Cash Equities requires strong core Java and Multithreading skills to build high performance, low latency Java application to process millions of orders per day.

2 ways to find if thread holds lock on object in Java

How to check if a thread holds lock in JavaHere I am giving my answer and what I had discovered after the interview. Yes, unfortunately, I didn't know the answer when Interviewer asked me during the telephonic round of interview. I managed to provide the logic based upon the properties of synchronized block and wait() and notify() method, as shown in the first answer but he wasn't satisfied at that time.

It's common for many interviewers to expect the answer they think is best and they push until you give up or you give them the answer they want.

1)I thought about IllegalMonitorStateException which wait() and notify() methods throw when they get called from non-synchronized context so I said I would call newspaper.wait() and if this call throws an exception it means thread in Java is not holding the lock, otherwise thread holds the lock.

How to check if a thread holds a lock in Java

2) Later I discovered that thread is a static method called holdsLock(Object obj) which returns true or false based on whether threads holds a lock on the object passed.

You can also check out Core Java Volume 1 - Fundamentals by Cay S. Horstmann or Java Threads 2nd Editon to learn more about Thread class in Java. The first book is one of the best books to learn secrets of Java in easiest possible manner.

How to find a thread has a lock in Java

That's all about how to find if a thread holds a lock on a particular object in Java. If you ever need to know this inside your Java application, you should use the holdsLock() method of java.lang.Thread class.

Comments, Suggestions ,innovative better answers are always welcome.

Other Java Threading tutorial you may like:

Further Learning
Multithreading and Parallel Computing in Java
Java Concurrency in Practice - The Book
Applying Concurrency and Multi-threading to Common Java Patterns
Java Concurrency in Practice Bundle by Heinz Kabutz


CARFIELD said...

How about using jconsole to check?

Javin @ Tibco RV Tutorial said...

Hi CARFIELD, yes jconsole can be used to monitor threads but that's not the programmatic way of doing it . you may be able to know which thread holds a lock by using ThreadMXBean in java 5 in a programe.

I have also discussed way to find out deadlock in java which is related to this. Please have a look there.

Javin @ Tibco RV Tutorial said...

Hi Gautam,

Yes Thread.holdslock() is the best way to figure out whether thread holds a lock on a particular object or not, let us know if you have some other ways to do it.


Kedar said...

Does anyone remember a situation where you want to check if a thread holds a particular lock or not? I have never used this method, neither that I have needed it. I have only been asked this question one time on a telephonic interview with HSBC technology India. I see the point of surprise but this question is not practical.

Unknown said...

Is there a way to know the object whose lock a thread is holding?

Anonymous said...

@Gaurav, I think there is no way you can find from Thrad class what type of object's lock it is holding. Also there is no method to find even which locks, how many locks theread is holding currently, let me know if there exists something like that even in Java 8.

Post a Comment