Monday, June 29, 2020

What is happens-before in Java Concurrency? An example

A couple of days ago, one of my readers messaged me on LinkedIn about a Java interview question he has recently faced - what is the happens-before relationship in Java concurrency? What is the benefit of it, and how exactly it works? He kind of has some ideas about that its related to the Java Memory Model and provides some sort of visibility guaranteed but couldn't explain with conviction to his interviewer, particularly with a code example and was a bit disappointed. He then asked me if I can write an article about it. I said you should have read the Java concurrency in Practice book before the interview, that would have helped, but nonetheless, I liked the idea to just provide a quick overview of what is the happens-before relationship between threads in Java.

To understand the happens-before relationship, you need to first understand what problems can occur if the same variable is accessed by multiple threads? particularly if one thread writes into the variable, and one thread read from it at the same time.

For example, let's say we have the following code, which is executed by Thread T1, (note, integer variable y is initialized before x)

int y = 1;
int x = 2;

Now, we have another piece of code which is executed by another thread T2 where the value of both the variable is printed (note variable x is printed before y) :

System.out.print(x);
System.out.println(y);

What do you think? What should be printed on the screen? If this code is executed by the same thread, then it's guaranteed that 2 and 1 will be printed, but with multiple threads, there is no guaranteed behavior. It's entirely possible that T2 could not see the assignments made by T1 and just print 0 for both x and y.

It's also possible that it can just see the initialization of either x or y and print accordingly.

This is scary and unpredictable, think what will happen if X is money in your bank account, you definitely want a predictable behavior. That's where happens-before comes into play. let's learn what is happens-before in Java and how it helps in writing predictable concurrent code.





What is Happens-Before Relationship in Java

The Happens-before relationship provides some sort of ordering and visibility guarantee. There is a lot of rule concerning Happens-before (which you can read on Java Concurrency in Practice). Still,  the most important one is if there is a synchronization like a synchronized block or a volatile variable then.

- "A volatile write will happen before another volatile read."
- "An unlock on the synchronized block will happen before another lock."

And, (this is most important)

All the changes which are visible to T1 before a volatile write or a synchronized unlock will be visible to thread T2 after a volatile read of the same variable or locking on the same monitor.

This statement is the most important one, and it can be best understood by the following diagram:

What is happens-before in Java Concurrency? example tutorial
Image credit - Java Concurrency in Practice

You can see that before unlock, thread T1 can see y=1 and x=1, so after T2 gets a lock on the same object, it can also see y=1 and x=1, even though y is not a volatile variable or read inside the lock or synchronized context. This is also true for volatile write and volatile read, which offers slightly less form of synchronization but a great alternative when it comes to visibility.

Now, let's go back to our example if we modify our code like this:

T1:
int y = 1;
volatile int x = 2;
 
T2:
System.out.print(x);
System.out.println(y)

What do you think? What does the code print now? 2 and 1 or 2 and 0?

Well, if you apply the happens-before than when T1 does a volatile write, and T2 does a volatile read, then it will also see the value of y=1, which is not volatile. Isn't behavior is now more predictable.

The critical thing to remember here is that even the value of a non-volatile variable is visible to thread T2. This is just one example of a happens-before relationship but very useful in analyzing the behavior of a multi-threaded program.

If you are serious about improving your understanding of Java concurrency knowledge and skills, I strongly suggest you should read the Java Concurrency in Practice book by Brian Goetz, Joshua Bloch, Doug Lea, and team, one of the best resources for Java developers.

best book to understand happens before in Java



But, If you find trouble understanding the book and need someone to explain to you those concepts with more examples, you can also join the Java Concurrency in Practice Bundle course by Heinz Kabutz which is based upon this book and makes it easy to understand those tricky concurrency concepts. It's like someone explaining your Java Concurrency in Practice book live.

best course to learn Java Concurrency in Practice.


Even if you have read the book, going through this course will help you to understand Java Concurrency better and help you to write robust concurrent code with fewer bugs. Sorry, it's almost impossible to write concurrent code without subtle bugs, at least in the first iteration.

The only thing is that some of you may find the course not affordable. There are options to make interest free part payments and if you can, you should that. But, if that's not the case then you can also check out the more affordable course on Udemy and Pluralsight they provide an excellent alternative to learn Java concurrency better.

Further Learning
The Complete Java Masterclass
Multithreading and Parallel Computing in Java
Java Concurrency in Practice - The Book
Applying Concurrency and Multi-threading to Common Java Patterns


Other Java Concurrency Articles you may like
  • The 2020 Java Developer RoadMap (roadmap)
  • 10 Java Multithreading and Concurrency Best Practices (article)
  • Top 50 Multithreading and Concurrency Questions in Java (questions)
  • Top 5 Books to Master Concurrency in Java (books)
  • Difference between CyclicBarrier and CountDownLatch in Java? (answer)
  • How to avoid deadlock in Java? (answer)
  • Understanding the flow of data and code in Java program (answer)
  • Is Java Concurrency in Practice still valid in 2020 (answer)
  • How to do inter-thread communication in Java using wait-notify? (answer)
  • 10 Tips to become a better Java Developer in 2020 (tips)
  • 5 Courses to Learn Java Multithreading in-depth (courses)
  • 10 Advanced books for Experienced Programmers (books)
  • 50+ Thread Interview Questions for Beginners (questions)
  • Top 5 skills to Crack Coding interviews in 2020 (article)
  • 10 Advanced Core Java Courses for Experienced Programmers (courses)

Thanks for reading this article so far. If you like this article or thread interview questions, please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P.S. - If you are new into Java Multithreading and looking for a free online training course to learn Multithreading and Concurrency basics then I also suggest you check out this Java Multithreading a free course on Udemy. It's completely free and all you need is a free Udemy account to join this course. 

No comments :

Post a Comment