August 15, 2016
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.
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.
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.