Search code examples
javamonitorsynchronized

How to know how much time cost by "synchronized" code, in Java?


I've a Java application, which is not fast enough as I expected. I've done a lot of searches of how to improve it, but not lucky.

Now I'm reviewing the code, and found there are a lot of synchronized keyword in the code. I'm thinking if they took too much time on waiting the locks.

Is there any tool to check how much time they cost? Thus I can find a better solution if they do cost too much time.


Solution

  • I was going to suggest the Thread Monitor feature of jvisualvm and, while looking for a screen shot, ran into this blogpost: Detecting Thread Contentions. It's way better than just a screenshot :).

    But here it is anyway: thread contention in jvisualvm (image credit: aforementioned blog post)