Tuesday, December 13, 2016

Is "Java Concurrency in Practice" still valid in era of Java 8?

One of my reader Shobhit asked this question on my blog post about 12 must read advanced Java books for intermediate programmers - part1. I really like the question and thought that many Java programmers might have the same doubt whenever someone recommends them to read Java concurrency in Practice. When this book came first in 2006, Java world was still not sure of about new concurrency changes made in Java 1.5, I think the first big attempt to improve Java's built-in support for multi-threading and concurrency. Many Java programmers were even not aware of new tools introduced in the API e.g. CountDownLatch, CyclicBarrier, ConcurrentHashMap and much more. The book offered them the seamless introduction of those tools and how they can use them to write high-performance concurrent Java applications.

This is the general view of the book, which many Java developer will give you when you asked them about how did you find "Java Concurrency in Practice", but mine view is slightly different from them and that's the main reason I still recommend Java Concurrency in Practice to any new Java developer or intermediate developers who want to master concurrency concepts.

The most important thing this book introduce was clear concepts and fundamentals of concurrent programming e.g. visibility, ordering, thread-safety, immutability, parallelism etc. It also goes on to explain why most concurrent applications are incorrectly written in Java and the common mistakes made by Java programmers which result in multi-threading issues like race conditions, deadlock, livelock, memory interference and simply incorrect calculation.

The emoticons it used to show the bad practice before introducing the good and right way to do helped a lot not only to correct the misconception many Java developer had before but to sow the seed of right information pertaining to multi-threading and concurrency among Java developers.

No doubt that multi-threading and concurrency is difficult, they are both hard to get it right in code and hard to understand and explain. I have seen many programmers who simply wasn't able to visualize how multiple threads are interacting with the same piece of code with different data. Much like the classical concept of recursion which is too easy for some programmers but too difficult for others to grasp and apply in real world scenario.

The biggest contribution of  Java Concurrency in Practice to the Java world is not making concurrency easy but providing the correct and clear information which was lacking. Since I have taken a lot of interviews, I know that programmers always has misconceptions about threads and how they work.

Many programmers even with 4 to 5 years of experience in Java didn't understand how volatile variable works, what they know is that when you use a volatile variable it always check the value from main memory while comparing, which is the truth but not complete.

They were not told about Java memory model and how volatile variable can affect the ordering of code and computing instructions behind code by JIT and JVM for optimization which could result in subtle logical bugs. They were not made aware of that how volatile can guarantee visibility of what was done by one thread prior to accessing volatile variable to other thread etc. They don't know what is a memory barrier and how does it impact the visibility.

The Java concurrency in Practice book teach those concepts to many Java programmer. In fact, I would admit that before reading that book, I have lots of misconceptions about many essential multi-threading and concurrency concepts e.g. ordering, visibility and subtle effect of final variables and safe-publication, the book helped me to clear those.

Is "Java Concurrency in Practice" still valid in  era of Java 8?

Now, coming back to Java 8, yes from Java 1.5 to Java 8, JDK have a lot more new tools to implement concurrency and design better Java concurrent application. The introduction of fork-join pool in JDK 7, CompleteableFutures in Java 8 and most importantly the new functional style of coding in Java 8 supported by the lambda expression.

You also got the stream and parallel stream which allows developers to take advantage of concurrency without coding it. The overall idea of moving concurrency from application developer to API developers also makes it little easier and reduced risk of implementing concurrency in Java. It also means that now you can perform the bulk operation in Java with just a couple of methods and multiple threads without writing a single line of code involving threads, synchronized keyword, or wait-notify methods.

No doubt, that Java developer has to learn these new tools to keep themselves up-to-date and a book like Java 8 in Action really helps on that front. It introduces you with all new changes in Java 8 and not only teaches you how to use them in your day-to-day task but also explains the motivation behind them to understand the bigger picture.

Best book to learn Java 8

Even though Java Concurrency in Practice in its current state doesn't cover all these important concepts and tools, it's still an invaluable book to learn the fundamentals of threads, concurrency and multi-threading tools supported by Java programming language.

It's still a must-read book for any Java developer who wants to learn and master multi-threading and concurrency, the biggest advantage of using Java for application development. I am thankful to Brian Goetz, Joshua Bloch and all the authors for giving Java developer such authority book to understand the confusing but critical concepts of multi-threading and concurrency.

Having said that, like many Java developers around the world, I would also love to see a newer, more up-to-date version of Java Concurrency in Practice to cover tools and methodologies introduced in Java 6, 7 and 8, much like the updated version of Head First design pattern which covers Java 8 and teaches you how certain patterns are easier to implement with new Java 8 features.

So, let's hope Brian Goetz, Joshua Bloch and other authors of the book listen to this request and gift Java developers around the world with a new version of Java Concurrency in Practice in 2017.

Further Learning
Java Fundamentals Part 1,2
Applying Concurrency and Multi-threading to Common Java Patterns


Unknown said...

Interesting article... As an intermediate Java programmer that has yet to learn how to use concurrency, I now see that perhaps it is a good idea to brush off the concepts and delve into Java 8 lambda expressions rather than train methodically to master concurrency and its best practices as introduced in Java 1.5.
If it is possible to have your feedback, I'd like to know what you think?

Javin Paul said...

Hello @unknown, I won't advise that. If you don't know how concurrency works, how Java memory model works, how thread interact with each other, what change they can see and what they cannot see, then you would face numerous challenges while coding, debugging or maintaining Java application. You should first learn those.

Also, lambda expression is independent of concurrency, you can still learn how to use that while learning concurrency and multithreading fundamentals.

Post a Comment