[Translation] Is the book “Java Concurrency in Practice” relevant to Java 8 and 11?

[Translation] Is the book “Java Concurrency in Practice” relevant to Java 8 and 11?

Hello, colleagues!

The article, the translation of which we offer today, once again reminds us of the importance of the ageless book " Java Concurrency in practice "by Brian Goetz.

Dates of comments to this article in the original suggest that the author updates and re-publishes it not for the first time. Therefore, we also allowed ourselves to update the link to the book of Raul-Gabriel Urma, Mario Fusco and Alan Mycroft mentioned in the article, which was published in Manning, under the name “Java 8 in Action”. We are preparing a translation of a new edition called Modern Java. But for now, let's talk about the classics. You are invited under the cat.

A reader under the name Shobhit asked me this question in the comments to the article on 12 Advanced Java Books for Intermediate Programmers - Part 1 . The question is really good, and I think many Java programmers had similar doubts when in our time someone recommended them to read “ Java Concurrency in Practice ”. When this book just came out, in 2006, the whole world of Java still could not figure out the innovations in the field of competition made in Java 1.5. I think then the first serious attempt was made to improve the built-in support for multithreading and competitiveness in Java. At that time, many programmers did not even know about the new tools that appeared in the API, for example, about CountDownLatch , CyclicBarrier , ConcurrentHashMap and many others. The book served them as a well-established introduction to working with these tools, telling them how to write high-performance competitive Java applications with their help.

This is the general idea of ​​this book, and many developers will describe it to you in this vein, if you ask, “And how do you like Java Concurrency in Practice?” However, I take this book a little differently, and that is why I still recommend it to all newbies who are familiar with Java, or mid-level developers who want to learn concepts related to competitiveness.

The most important material with which this book will introduce you is clear concepts and basics of competitive programming, in particular, visibility, ordering, thread safety , immutability , concurrency, etc.

She also explains why in most cases competitive Java applications are written incorrectly, why Java programmers make common mistakes that lead to problems with multithreading, including race conditions, dead blocking, active blocking, interference in memory, and simply incorrect calculations.

The book uses emoticons accompanying the description of "how to do wrong," before introducing the reader to the correct and high-quality solution to the problem. So the book not only helps to combat the misconceptions that many Java developers retain, but also to instill in the Java community the right information on how to work with multithreading and competitiveness.

Without a doubt, multithreading and competitiveness are complex. They are not easy to implement correctly in the code, it is equally difficult to understand and explain.I know a lot of programmers who are simply unable to visually depict how different threads interact with the same element of code using different data.

Like the classic example of recursion, one programmer can easily use multi-threading in practice, while others can hardly understand multi-threading and use in an application scenario.

The greatest contribution of the Java Concurrency in Practice book to the development of the Java world is not to simplify competitiveness, but to give correct and accurate information about it that was lacking. Since then, I have conducted many interviews and I know that programmers do not always quite accurately imagine the device of threads and how they work.

Many programmers, even those who have been working with Java for 4-5 years, do not understand, how they are arranged mutable variables ; all they know is that when working with a mutable variable, you need to check with each comparison which value is in the main memory. This is true, but only part of the truth.

They were not told about the Java memory model, about how a changeable variable can affect the order of the code and the execution of computational instructions at its core. It's about using dynamic compilation ( JIT ) and virtual Java machines ( JVM ) for optimization; such optimization can lead to subtle logical errors.

People are not told how changeable variables allow us to see what has been done in one thread, before accessing a variable from another thread, etc. Not everyone knows what a memory barrier is and how it affects visibility.

It is through the book Java Concurrency in Practice that many Java programmers have learned all these concepts. I have to admit that I myself, until I read it, was in many ways mistaken about many significant issues of multithreading and competitiveness, in particular, the order, appearance and implicit effects of the final variables and safe publication. The book helped me figure it all out.

By the way, if some sections of the book seem a little incomprehensible to you, believe me, not to you alone. Here you should thank Dr. Heinz Kabutz (Heinz Kabutz), who presented the material of the book in a simplified form in his course Java Concurrency in Practice Bundle .

Even if this material seems too complicated, Heinz has another course, Mastering Threads , which helps the average Java programmer understand the multithreading.

Now let's talk about Java 8, that is, what has changed from Java 1.5 to Java 8. The JDK has a lot of new tools for introducing competition and designing better-quality competitive Java applications. In JDK 7, the fork-join pool appeared in Java 8 - CompleteableFutures . More importantly, starting with Java 8, a new, more functional programming style has become established, provided by lambda expressions .

We also have streams (streams) and parallel streams (parallel streams), allowing developers to take advantage of competitiveness without programming it.The whole idea of ​​taking the implementation of competition from application developers and reassigning it to API developers slightly simplifies the competitive situation in Java and reduces the risks associated with its implementation.

It also means that in Java, you can perform bulk operations on multiple threads with just a couple of methods, and not write a single line of code associated with threads using the synchronized keyword or wait-and-notify (wait-notify) methods.

Undoubtedly, any Java developer needs to explore these new tools in order to keep up with the development of technology - in which, of course, a book like “ Modern Java In Action ." It will not only introduce you to all the innovations of the Java language, but also help you learn how to use them when performing everyday tasks, understand the motivation of these innovations and get a general idea of ​​the modern Java language.

Although the Java Concurrency in Practice book in its current form does not cover all of these important concepts and tools, it still remains invaluable for learning the key features of the Java language related to streams , competitiveness and multi-threading .

The Goetz book is still a must-read for any Java developer who wants to learn and master multithreading and competitiveness - the most powerful aspects of Java when developing applications.

With all this in mind, I, like many Java developers around the world, would also like to see the updated edition of Java Concurrency in Practice, where the tools and methodologies introduced in Java 6, 7, 8, 9, 10 and maybe even in Java 11. After all, there were updated versions of Effective Java and Head First design patterns reviewing Java 8 and demonstrating how much easier it is to implement various patterns using the new features of Java 8 .

Source text: [Translation] Is the book “Java Concurrency in Practice” relevant to Java 8 and 11?