Summary

The strong integration of locks into the Java language and API is very useful for programming with Java threads. Nonetheless, despite their strength, Java’s locking mechanisms are not suitable for every type of synchronization you might need for more complex Java programs. Fortunately, the built-in synchronization techniques provide good building blocks to create the more complicated, more intelligent locks you need in special situations.

Like other parts of Java, its built-in locking mechanism is designed to be simple in order to reduce errors in your Java programs. And, like other parts of Java, this simplicity is enough to carry you through all but the most complex programming situations. You should use the built-in techniques unless you really need the more complex behavior of the mechanisms described in this chapter.

Finally, for those times when you are faced with other code that is not thread safe, Java’s locking facilities offer the ability to use that code safely within a multithreaded program, either by explicitly locking such code or by ensuring that such code is only ever executed within a single thread.

Get Java Threads, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.