Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Synchronized Methods Slower In Single Threaded Applications?

I've been debating this to myself for the last few minutes, and I'm seeing reasons for both yes and no. This stemmed from looking at the answers to Java HashMap vs. Hashtable and seeing several people say Hashtable is in fact slower.

It seems to me that a synchronized method should act absolutely no different than its unsynchronized counterpart if running in a single thread, since the action of synchronizing shouldn't block anything. That said, I would imagine the compiler handles the two cases differently and that's why people are saying synchronized is slower.

Not that it's by any means conclusive, but I ran some simple tests on HashMap vs Hashtable, and saw little difference in speed.

like image 666
dimo414 Avatar asked Jun 10 '09 03:06

dimo414


People also ask

Which is more efficient synchronized method or synchronized block?

A Java synchronized block doesn't allow more than one JVM, to provide access control to a shared resource. The system performance may degrade because of the slower working of synchronized keyword. Java synchronized block is more efficient than Java synchronized method.

What are the advantages of using a lock instead of synchronization?

In synchronisation, if a thread is waiting for another thread, then the waiting thread won't do any other activity which doesn't require lock access but with lock interface there is a trylock() method with which you can try for access the lock and if you don't get the lock you can perform other alternate tasks.

What is the advantage of thread synchronization?

It prevents the thread interference and inconsistency problem. Synchronization is build using locks or monitor.

Why synchronization is necessary in multi threaded programming?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.


2 Answers

Yes, single-theaded Java programs that use synchronization may be slightly slower than they would be without synchronization. For early Java releases, synchronization was expensive. For any modern release, however, uncontended synchronization is pretty cheap. I wouldn't worry about this.

Note that Java 6 has and Java 7 is to have good optimizations around locking:

  • Lock coarsening
  • Lock elision
  • Adaptive Spin locking
  • Biased locking

For more information, see the Java SE 6 Performance White Paper. Also note that uncontended synchronization appears to be more expensive on multi-core CPUs than on single-core CPUs, perhaps due to the Java Memory Model requirements of synchronization forcing local CPU caches to be shared with other CPUs, or some other memory barrier. For example, read Do Java 6 threading optimizations actually work? - Part II. (The Part I was not as insightful as the Part II.)

like image 180
Eddie Avatar answered Sep 28 '22 06:09

Eddie


Yes. They are going to be slightly slower due to the extra overhead of maintaining locks.

like image 23
Mitch Wheat Avatar answered Sep 28 '22 06:09

Mitch Wheat