Think about a scenario where you would have to find at the run time whether a Java thread has a lock on a particular object e.g. find out whether thread NewsReader has a lock on a NewsPaper object or not? If these questions came in any core java interview then I would automatically assume that there could be at least two answers one is the hard-earned raw answer which programmer would like to figure out based on fundamentals and the other could be some rarely used API calls that are available in Java, by the way, this is actually asked to me in an interview of one of the biggest global Investment bank.
The investment banking world, particularly Cash Equities requires strong core Java and Multithreading skills to build high-performance, low latency Java applications to process millions of orders per day.
And, if you are keen to learn more about Java multi-threading and concurrency then you can also check out these advanced Java Multithreading courses for intermediate and experienced Java developers. It contains advanced courses to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance.
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 the newspaper.wait() and if this call throws an exception it means thread in Java is not holding the lock, otherwise thread holds the lock.
2. Later I discovered that thread is a static method called holdsLock(Object obj) which returns true or false based on whether threads hold a lock on the object passed.
You can also check out these Java Programming courses for Beginners to learn more about Thread class in Java. The first course is one of the best courses to learn the secrets of Java in the easiest possible manner.
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.
The investment banking world, particularly Cash Equities requires strong core Java and Multithreading skills to build high-performance, low latency Java applications to process millions of orders per day.
And, if you are keen to learn more about Java multi-threading and concurrency then you can also check out these advanced Java Multithreading courses for intermediate and experienced Java developers. It contains advanced courses to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance.
2 ways to find if a thread holds a lock on an object in Java
Here 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 interviews. I managed to provide the logic based upon the properties of the 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 the newspaper.wait() and if this call throws an exception it means thread in Java is not holding the lock, otherwise thread holds the lock.
2. Later I discovered that thread is a static method called holdsLock(Object obj) which returns true or false based on whether threads hold a lock on the object passed.
You can also check out these Java Programming courses for Beginners to learn more about Thread class in Java. The first course is one of the best courses to learn the secrets of Java in the easiest possible manner.
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.
Further Learning
And lastly, let me know if you have been asked this question before? or what is your favorite Java multithreading interview question?
How about using jconsole to check?
ReplyDeleteHi 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.
ReplyDeleteI have also discussed way to find out deadlock in java which is related to this. Please have a look there.
Hi Gautam,
ReplyDeleteYes 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.
Thanks
Javin
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.
ReplyDelete@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.
ReplyDelete