From the course: Parallel and Concurrent Programming with Java 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Read-write lock

Read-write lock - Java Tutorial

From the course: Parallel and Concurrent Programming with Java 1

Start my 1-month free trial

Read-write lock

- We use a locker mutex to protect a critical section of code to defend against data races, which can occur when multiple threads are concurrently accessing the same location in memory and at least one of those threads is writing to that location. That second part is key, because if we have a bunch of threads and none of them are writing, they are all just want to read from the same location, that's fine. It's okay to let multiple threads read the same shared value as long as no one else can change it. They'll all safely see the same thing. Danger only exists when you add a thread that's writing to the mix. When we use a basic lock or a mutex to protect the shared resource, we limit access so that only one of the threads can use it at a time, regardless of whether that thread is reading, or writing, or both. - That works but it's not necessarily the most efficient way to do things, especially when there are lots of threads that only need to read. This is where reader-writer locks can…

Contents