Lecture 7 - Threads & Java memory model
In this lecture, we've looked at the basic API for the java.lang.Thread, so you can spawn new threads and make them do the computations. We've looked at how one can stop them, use them to manipulate shared mutable state, wait for threads to finish, etc.
We've talked about the Java Memory Model, and how it formalizes what we'd like to formalize about what values different threads can see when they read the fields of your objects.
We've touched on the atomicity, the rules of the visibility for the operations, how the synchronization on the same object makes your writes to the data visible in the other threads.
One takeaway that I want you to remember, if you're using multiple threads to manipulate some data, either mark the fields volatile, so write / read pairs would ensure the visibility of the other writes. Use synchronization when you need to ensure the mutual exclusion.
If you'd like to learn more, which I strongly encourage you to do, start with the following resources by :
- Java Memory Model Pragmatics by Aleksey Shipilev
- Video of the Java Memory Model Pragmatics by Aleksey Shipilev
- Close Encounters of The Java Memory Model Kind by Aleksey Shipilev
- Lecture slides: 7-threads-jmm.pdf
- Homework: https://github.com/shelajev/jf-skeleton (also in the lecture slides)
- Deadline for the homework: Oct 17, 20:59:59 UTC (23:59:59 Tartu)