views:

478

answers:

5

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.

+7  A: 

Maybe it's not as bad as you think

Here is a good article about it

gnibbler
+3  A: 
OscarRyz
+5  A: 

This article over at IBM actually summarizes very nicely the main points behind 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 unsynchronized 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.

Vincent Ramdhanie
Please be aware that the newest Java version that article quotes is Java 1.3. That version was released in May 2000 and has long since been EOLed. While the basic facts remain somewhat similar (the Java Memory Model changed some of it), there have been tons of optimizations that make synchronization very cheap in the unontested case for example.
Joachim Sauer
s/unontested/uncontested/ obviously.
Joachim Sauer
And, of course, both mutual-exclusion of code blocks, and coherency of memory are fundamentally necessary when writing multithreaded code, and there are no "clever" tricks (only subtly broken ones) in Java to avoid them. If your two threads have to interact, you have to synchronize - pure and simple. Granularity synchronization is the only question remaining.
Software Monkey
+3  A: 

This isn't that specific to Java. Synchronization can be considered "expensive" in any multi-threaded environment if not done correctly. Whether it's particularly bad in Java, I do not know.

It prevents threads from running concurrently if they use the same resource. But, since they do use the same resource, there's no better option (it has to be done).

The problem is that people often protect a resource with too big a scope. For example, a badly designed program may synchronize an entire array of objects rather than each individual element in the array (or even a section of the array).

This would mean that a thread trying to read element 7 must wait for a thread reading or writing element 22. Not necessary. If the granularity of the synchronization were at the element level instead of the array level, those two threads wouldn't interfere with each other.

Only when two threads tried to access the same element would there be resource contention. That's why the general rule is to only protect as small a resource as possible (subject to limitations on number of synchronizations, of course).

But, to be honest, it doesn't matter how expensive it is if the alternative is data corruption due to two threads fighting over a single resource. Write your application correctly and only worry about performance problems if and when they appear ("Get it working first then get it working fast" is a favorite mantra of mine).

paxdiablo
+2  A: 

The other answers give a good level of technical detail that I'm not going to attempt to replicate.

What I will do is advise you to check the dates of the articles (as well as the implied competence and awareness of the author). Synchronization in Java was very slow in earlier JVMs. However, it's improved a lot recently, such that uncontended synchronization is a lot faster than you might think, and uncontended synchronization has improved too.

Mind you, this question possibly doesn't matter - if you need to synchronize to ensure correctness, you need to synchronize to ensure correctness. The only times I can see the speed being an issue is if you were considering creating a lockless implementation instead (using the very efficient yet complex java.util.concurrent.locks.AbstractQueuedSynchronizer), or perhaps considering using another language for your task instead.

In general I think the best conclusion is that synchronization is generally fast enough to use on a first iteration. As with all performance concerns, code for clarity and correctness at first and then only optimise what you measure to be an expensive part of your application. Typically, this won't be the cost of synchronization*.

Andrzej Doyle