Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are synchronize expensive in Java?

I am really new to Java and I read that synchronized is "very expensive" in Java. All I want to know is what is expensive and how is it expensive?

Thanks.

like image 907
unj2 Avatar asked Nov 04 '09 00:11

unj2


People also ask

What is the problem with synchronization in Java?

Synchronization can result in hold-wait deadlock where two threads each have the lock of an object, and are trying to acquire the lock of the other thread's object. Synchronization must also be global for a class, and an easy mistake to make is to forget to synchronize a method.

What is the disadvantage of synchronization in Java?

In simple two lines Disadvantage of synchronized methods in Java : Increase the waiting time of the thread. Create performance problem.

Why synchronized methods are slower?

First of all, making the methods synchronized is not necessary since they do not modify anything outside of the method itself. In fact it prevents both threads from running at the same time as the methods are static therefore preventing two thread from being in either of them at the same time.

Is synchronized method more efficient?

If you synchronize a code block within that method then more than one thread can execute the method simultaneously, but only one thread can enter the synchronized block at a time. From this we can conclude that synchronizing on the smallest possible code block required is the most efficient way to do it.


2 Answers

Maybe it's not as bad as you think

It used to be terrible (which is possibly why you read that it was "very expensive"). These memes can take a long time to die out

How expensive is synchronization?

Because of the rules involving cache flushing and invalidation, a synchronized block in the Java language is generally more expensive than the critical section facilities offered by many platforms, which are usually implemented with an atomic "test and set bit" machine instruction. Even when a program contains only a single thread running on a single processor, a synchronized method call is still slower than an un-synchronized method call. If the synchronization actually requires contending for the lock, the performance penalty is substantially greater, as there will be several thread switches and system calls required.

Fortunately, continuous improvements in the JVM have both improved overall Java program performance and reduced the relative cost of synchronization with each release, and future improvements are anticipated. Further, the performance costs of synchronization are often overstated. One well-known source has cited that a synchronized method call is as much as 50 times slower than an un-synchronized method call. While this statement may be true, it is also quite misleading and has led many developers to avoid synchronizing even in cases where it is needed.

Having said that - concurrent programming can still be slow, but not so much of it is purely Java's fault now. There is a trade-off between fine and coarse locking. Too coarse is obviously bad, but it's possible to be too fine too, as locks have a non zero cost.

It's important to consider the particular resource under contention. Mechanical hard disks are an example where more threads can lead to worse performance.

like image 62
John La Rooy Avatar answered Sep 25 '22 23:09

John La Rooy


It is expensive because if you are using threads, and a number of threads have to go through a synchronized section of code, only one of them may be executed at a time.

It is like a bottleneck.

It is even expensive when you use a single thread, because it has to check anyway if he is allowed to run.

If you reduce the use of synchronized segments your thread won't have to stop to see if they can run ( of course, they don't have to share data )

A high level overview of how synchronization works may be found here

http://img20.imageshack.us/img20/2066/monitor28synchronizatioc.png

A Java style monitor

like image 34
OscarRyz Avatar answered Sep 23 '22 23:09

OscarRyz