The new JDK™ 9 early access release contains a JDK enhancement proposal JEP 143, Improve Contended Locking, to improve the performance of contended monitors. Monitors are used by the java synchronized statement to lock the access to a code block. If the synchronized block is called by many threads, the monitor becomes contended. This can degrade the performance dramatically. So let us look at the performance improvements of contended monitors.
The graphic shows the time of one method call. Lower means better performance. The test consists of 8 threads accessing a synchronized block. All threads are accessing the same monitor. You can download the test here. The test was run on an intel i5 4 core CPU
As we see JDK 9 improves the performance of contended monitors. Let us now look at a direct comparison between JDK 8 and 9.
Comparison between JDK 8 and JDK 9
The following shows how much the switch from JDK 8 to JDK 9 will bring.
The graphic shows the time of one method call for a contended monitor at different threads. As we see the performance of JDK 9 degrades mutch slower than the performance from JDK 8. For 16 Threads JDK 8 needs 2580 ns while JDK 9 only needs 1655 ns. This is an improvement by more than 60 percent.
One advice to improve the performance is to use reentrant locks instead of synchronized blocks. So let us see if this advice is still true for JDK 9.
Reentrant Locks vs synchronized
Let us look at the performance of a contended reentrant lock vs a contended monitor of a synchronized block.
The graphics show the time of one method call at different threads. As we see in JDK 9 the performance of the synchronized statements gets almost as fast as reentrant locks.
As we have seen JDK 9 improves the performance of contended monitors. In JDK 9 contended monitors are almost as fast as contended reentrant locks. But JDK 9 also has a JDK enhancement proposal JEP 285, Spin-Wait Hints, to improve the performance of locks. I will look at this in the next blog article at this blog.
If you have a question or remark please add a comment below.