Synchronizing Methods

A few days ago someone with more experience, and I assume more knowledge about the Java Virtual Machine told me that there actually is a difference between a synchronized method and a synchronized(this) block encapsulating the entire method body. The reason why he used the latter was to get more performance, as so he told, the JVM is allowed to synchronize all methods (even those without the synchronized keyword) if this makes it easier to manage. The gained performance comes from methods that do not need to be synchronized but will be out of your control. As a good computer scientist, I tried to test the statement.

Like every quick test, it is a bit of an artificial piece of code. The code consists of a 2 methods that does some computation and stores it in a member, another one will just return the value of the member. These three methods are synchronized while another fourth just returns a simple string and is therefor not synchronized.

A first scenario consists of 100 threads trying to access the methods in sequence for 1000 times. The idea was that if the last method gets blocked by the other methods the complete execution time will increase. I did not measure the execution time of the separate methods, which probably would have been a good idea. The results however are inconclusive as for both methods the average runtime is around 230 seconds.

A second scenario tried to push the non-synchronized method more by calling it 1000 times for each cycle. The results are a bit confusing as both techniques where faster while executing more code. The difference is still very small with the synchronized methods coming in at 220 seconds, and the synchronized blocks at 215. Which means the synchronized blocks was 2% faster.

In my opinion the speedup (which still fits within the margin of error) does not justify the extra code clutter by having an extra synchronzed(this) block. The difference in speed may be different for more complex code, which I will investigate if I have a real world example.

There are however other arguments to either use a synchronized method or a synchronized block. It is clear that with a synchronized block you can lock different methods (or parts) on different objects. This allows for more performance than locking all methods on the same object, it does however make it a lot harder to avoid deadlocks.

So it just comes down to better performance against ease of use and complexity. It is much easier to just lock everything on the same object, and this might be sufficient but if you experience bad performance because of synchronization you will have to reduce the lock scope.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.