Race conditions is a situation that can occur when two or more threads access the same variables in a way where thread scheduling may affect the final result. The two threads "race" over access to the variables. There are two primary access patterns which can cause race conditions: Check-then-act and read-modify-write. I cover both of these race conditions causes in this video.
A section of code where race conditions can occur is called a critical section. To secure critical sections they must be made atomic. I show a few examples of how to do that in this video too.
Race conditions in concurrent programming can easily lead to concurrency errors in your applications, so it is important to understand what race conditions is, and how to avoid it!
Chapters:
0:00 Race Conditions introduction
0:54 Read-modify-write race condition example
8:06 Read-modify-write solution with atomic, synchronized block
9:47 One thread writes, one thread reads - example
13:27 Two threads that access same objects, but not write to the same objects.
16:24 Check-then-act race condition example
20:24 Check-then-act solution with atomic, synchronized block
Race Conditions - text:
http://tutorials.jenkov.com/java-concurrency/race-conditions-and-critical-sections.html
Java Concurrency tutorials - text / video:
http://tutorials.jenkov.com/java-concurrency/index.html
https://www.youtube.com/playlist?list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4
Java Memory Model - The Basics - text / video:
http://tutorials.jenkov.com/java-concurrency/java-memory-model.html
https://www.youtube.com/watch?v=LCSqZyjBwWA&list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4&index=3&t=803s
Java Happens Before Guarantee - video:
https://www.youtube.com/watch?v=oY14UyP61F8&list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4&index=4
Java Synchronized - text / video:
http://tutorials.jenkov.com/java-concurrency/synchronized.html
https://www.youtube.com/watch?v=eKWjfZ-TUdo&list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4&index=5
Java Volatile - text / video:
http://tutorials.jenkov.com/java-concurrency/volatile.html
https://www.youtube.com/watch?v=nhYIEqt-jvY&list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4&index=6
Java Lambda Expressions - text / video:
http://tutorials.jenkov.com/java/lambda-expressions.html
https://www.youtube.com/playlist?list=PLL8woMHwr36HQhhPPdV_T8rigbuywMpD7