Performance Improvements of Java Monitor From JDK 6 to 9
Looking at how the JDK has improved the performance of contended monitors over the years, especially the huge leap from 8 to 9.
Join the DZone community and get the full member experience.Join For Free
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 access to a code block. If the synchronized block is called by several threads, the monitor becomes contended. This can degrade performance dramatically. So let us look at the performance improvements of contended monitors.
The graphic shows the time of one method call. The lower numbers mean better performance. The test consists of 8 threads accessing a synchronized block, and 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 can 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 improvement 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.
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 is almost as fast as the 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 article on this blog.
Besides testing the performance of multithreaded code you should also test the correctness of your multithreaded code. Read more about a new way to unit test your multithreaded application.
If you have a question or remark please add a comment below.
Published at DZone with permission of Thomas Krieger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.