Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 822
Freewind Avatar asked Oct 22 '11 10:10

Freewind


2 Answers

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)

like image 92
Barend Avatar answered Oct 01 '22 07:10

Barend


A good profiler will be able to pinpoint the methods that are causing slowness it may not be related to the synchronization at all. Also looking at the state of the threads in profiler may reveal if there is too much of waiting going on because of the synchronization. In any case unless the synchronized keyword is being used without reason it may be counter productive to remove it for the sake of performance.

like image 27
Ashwinee K Jha Avatar answered Oct 01 '22 07:10

Ashwinee K Jha