Summary

It is hard to code and debug in a parallel environment. Java can give us some help here, with its built-in mechanisms for synchronization and the data structures in the JDK. As synchronization is integrated into the Java language, using it comes naturally. Every object in Java may be a monitor object and used for synchronization. The downside is that if it is too easy to use synchronization, it might be used unnecessarily.

This chapter also covered the Java Memory Model that is meant to unify the parallel behavior of multithreaded Java across all hardware architectures, as well as the problems therein.

We covered the implementation of synchronization and threads in a virtual machine and went over the most common ways of implementing adaptive locks, using thin locks and fat locks. In an adaptive runtime, locks can be inflated or deflated, based on runtime feedback, turning one into another depending on contention level.

Some basic optimizations were explained; most notably the lazy unlocking framework and the shortcuts the code generator can provide, by running lock fusion.

At the end of the chapter, some common caveats were reviewed, the most important ones being the usage of the deprecated methods Thread.stop, Thread.suspend and Thread.resume and the anti-pattern known as double checked locking. The biggest trap of all when writing complex multithreaded Java applications is to not understand the Java Memory Model.

Finally, common JRockit command-line flags for instrumenting and examining threads and synchronization, as well as modifying the behavior of the JVM were introduced.

The next chapter will talk about the necessity of benchmarking an application, both in order to guard against regressions and to provide information that can be used for performance improvements. Benchmarking and instrumentation can only be done well if you are equipped with a good understanding of JVM internals and of how Java works. Hopefully, enough fundamentals for this, on code generation, memory management, and threads and synchronization has been provided by the chapters so far.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.36.38