views:

53

answers:

3

I am trying to reduce the lock contention on a particular code-path in my application. To identify code with high lock contention, I connect YourKit to my application and use the "Monitor" tab and see acquiring which locks have caused the thread I care about to block. My eventual aim is to have no red dots for this thread in the "Thread" tab in YK GUI.

Often the contention arises from read/read locking using an intrinsic lock, so those can be improved by using a ReentrantLock instead of the intrinsic lock. Indeed when I tried this, YourKit reported lower contention. Is that information reliable? Does YourKit report usage of ReentrantLock's correctly?

I am using YourKit 8.0.24 on Solaris 10 with Sun 1.6u18 32-bit JVM.

A: 

If youre using Java 1.5 you may be seeing accurrate results. When ReentrantLock's came out in Java 5 they had a higher throughput rate of lock acquisition and releasing. So the lower contention may be a product of faster lock implementations.

However, when 1.6 came out intrinsic locking became comporable to j.u.c.Lock locking, so if 1.6 is the case then you may want to consider the YourKit itself.

John V.
John, Thanks that is a good point. I am on 1.6. I edited the question with software versions I am using.
binil
A: 

Instead of following a locking-approach try to design your application be lockfree. With volatile variables and the classes in java.util.concurrent you can often write algorithms that are lockfree and therefore have no lock contention.

Tobias P.
Tobias, I am sure you know very well that that is much harder! :) Yes, eventually I would like to do just that, using more immutable objects and possibly trading memory footprint for latency. But in the meantime, I was thinking that I could get rid of the read/read lock contentions and get better results than what I have now.
binil
Yes i know, not easy but will result in much better performance.The book "Java Concurrency in practice" is really good to learn more aspects of mostly lockfree programming.
Tobias P.
A: 

Looks like YourKit, as of now, does not report ReentrantLocks as blocked in the "Thread" tab.

binil