[ACCEPTED]-Are Synchronized Methods Slower In Single Threaded Applications?-synchronization
Yes, single-theaded Java programs that use 16 synchronization may be slightly slower than 15 they would be without synchronization. For 14 early Java releases, synchronization was 13 expensive. For any modern release, however, uncontended synchronization 12 is pretty cheap. I wouldn't worry about 11 this.
Note that Java 6 has and Java 7 is 10 to have good optimizations around locking:
- Lock coarsening
- Lock elision
- Adaptive Spin locking
- Biased locking
For 9 more information, see the Java SE 6 Performance White Paper. Also note that 8 uncontended synchronization appears to be 7 more expensive on multi-core CPUs than on 6 single-core CPUs, perhaps due to the Java 5 Memory Model requirements of synchronization 4 forcing local CPU caches to be shared with 3 other CPUs, or some other memory barrier. For 2 example, read Do Java 6 threading optimizations actually work? - Part II. (The Part I was not as 1 insightful as the Part II.)
Yes. They are going to be slightly slower 2 due to the extra overhead of maintaining 1 locks.
When using synchronized data structures, the 14 slowdown is not dependent on "how much" is 13 getting blocked. The act of acquiring or 12 releasing a lock is slow, as it usually 11 involves something like a system call (context 10 switches are slow on any platform). In a 9 JIT environment like a typical JVM, it would 8 theoretically be possible to optimize out 7 all lock/unlock calls when there is only 6 a single thread running, but it would have 5 to be properly invalidated whenever another 4 thread starts up.
Note that things like 3 Linux's futexes don't have to make system 2 calls unless there is contention, but using 1 them is still slower than a no-op.
More Related questions