Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Performance Improvements of Java Monitor From JDK 6 to 9

DZone's Guide to

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.

· Java Zone
Free Resource

Never build auth again! The Okta Developer Platform makes it simple to implement authentication, authorization, MFA and more in Java applications. Get started with the free API.

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.

Contended Monitor Performance for Different JDKs

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.

jdk8_vs_jdk9

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.

monitor_vs_lock_8

monitor_vs_lock_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 is almost as fast as the reentrant locks.

Conclusion

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.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
java 9 ,java 8 ,java

Published at DZone with permission of Thomas Krieger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}