Think about a scenario where you would have to find at run time that whether a java thread has lock on a particular object e.g. find out whether thread NewsReader has 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 answer 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 is available in java , by the way this is actually asked to me in an interview of one of the biggest global bank.
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 exception it means thread in java is not holding lock, otherwise thread holds lock.
2)Later I discovered that thread is a static method called holdsLock(Object obj) which returns true or false based on whether threads holds lock on object passed.
Comments, Suggestions ,innovative better answers are always welcome.
If this questions came in any core java interview then I would automatically assume that there could be at least two answer 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 is available in java , by the way this is actually asked to me in an interview of one of the biggest global bank.2 ways to find if thread holds lock on object in Java
Here I am giving my answer and what I had discovered after interview1)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 exception it means thread in java is not holding lock, otherwise thread holds lock.
2)Later I discovered that thread is a static method called holdsLock(Object obj) which returns true or false based on whether threads holds lock on object passed.
Comments, Suggestions ,innovative better answers are always welcome.
Other Java Threading tutorial you may like:
9 comments:
Good experience :) nice to know about we can know about thread holding lock can be identified.
Good find. Typically one does not need to this.
How about using jconsole to check?
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.
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.
Thanks
Javin
Hi Javin,
I didn't say there is better way to do this. I just meant typically one doesn't need to know this. Still it is a good article.
thanks
Excellent effort to give clarity.but it is the rare case.
Excellent effort to give clarity.
Post a Comment