Overview
There are a number of articles on whether synchronized or Locks are faster. There appear to be opinions in favour of either option. In this article I attempt to show how you can achieve different views depending on how you benchmark the two approaches.I have included AtomicInteger to see how a volatile field compares.
What are some of the differences
The synchronized keyword has naturally built in language support. This can mean the JIT can optimise synchronised blocks in ways it cannot with Locks. e.g. it can combine synchronized blocks.The Lock has additional method such as tryLock which is not an option for a synchronized block.
The test
As the JIT can optimise synchronized in ways a Lock cannot, I wanted to have a test which might demonstrate this and one which might "fool" the JIT.In this test, 25 million locks were performed between each of the threads. Java 6 update 26 was used.
Threads | 1x synch | 1x Lock | 1x AtomicInteger | 2x synch | 2x Lock | 2x AtomicInteger |
---|---|---|---|---|---|---|
1 : | 0.937 | 0.786 | 0.400 | 1.532 | 1.484 | 0.569 |
2 : | 2.766 | 4.597 | 0.676 | 5.398 | 6.355 | 1.278 |
4 : | 3.904 | 1.267 | 0.694 | 6.330 | 2.657 | 1.354 |
8 : | 3.884 | 0.953 | 1.011 | 5.418 | 2.073 | 2.761 |
16 : | 3.207 | 0.869 | 1.171 | 4.817 | 1.656 | 2.800 |
32 : | 3.213 | 0.853 | 1.240 | 4.915 | 1.680 | 2.843 |
64 : | 3.322 | 0.921 | 1.269 | 5.049 | 1.639 | 2.843 |
These are the times to perform 25 million locks/operation in seconds. Lower numbers are better. Different system will get different results, so these should be taken as a relative performance.
Note: It appears that Lock performs best with high numbers of threads. However this may because the performance approaches the single threaded performance. It may do this by avoiding contention and letting just one thread run for long periods of time.
The Code
SynchronizedVsLockTest.javaConclusion
In general, unless you have measured you system and you know you have a performance issue, you should do what you believe is simplest and clearest and it is likely to performance well.These results indicate that synchronized is best for a small number of threads accessing a lock (<4) and Lock may be best for a high number of threads accessing the same locks.
Related articles
Java Concurrency in practice - Favours locks for performance, otherwise suggest synchronized be used if not an issue. States Lock are much faster for Java 5.0, slightly faster for Java 6Java Performance: synchronized() vs Lock - Favours synchronized, based on a micro-benchmark. Compares one and two threads
java.util.concurrent ReentrantLock vs synchronized() - which should you use? - Favours synchronized on technical arguments
Synchronized vs. Lock vs. fair Lock - Compares locks based on fairness rather than performance
synchronized vs custom locking: Java synchronization performance - Determines that Locks were 5x faster for Java 5.0
Java synchronized block vs ReentantLock performance - Compares synchronized vs Locks with 10 threads, concludes Locks are significantly faster (about 5x) Not clear if this test was done with Java 5.0 also
Performance of ReentrantLock and Synchronized - Compares different numbers of threads and concludes synchronized is only fastest for one thread
No comments:
Post a Comment