In this video, learn how to use Java's ReentrantReadWriteLock class to enable multiple threads to simultaneously read within a critical section, while only allowing one thread at a time to write with an example Java program.
- [Instructor] To demonstrate using read/write locks … in Java, I've created this example program … in which 10 threads are reading what day it is … from a shared calendar, while two other threads … concurrently change the date. … The CalendarUser class has a constant array … of strings to represent the days of the week, … on line nine, as well as an integer … to indicate which day today is. … In the reentrant log named marker, … which will act as our mutex … to control access as threads read … and change the value of the today variable. … Down in the CalendarUsers run method, … inside of the while loop on line 18, … I'll execute one of two cases, … depending on whether the name I assign … this CalendarUser object indicates … that it's a reader or a writer. … The if statement on line 19 checks to see … if this CalendarUser is a writer thread, … and, if so, the thread will acquire the marker.lock, … change the calendar to the next day, on line 22, … it prints a message about doing so, and then, finally, …
- Parallel vs. sequential computing
- Shared vs. distributed memory
- Execution scheduling
- Thread lifecycle
- Mutual exclusion
- Synchronized methods
- Checking for liveness
Skill Level Advanced
Java Essential Training: Objects and APIswith David Gassner2h 46m Intermediate
Java Essential Training: Syntax and Structurewith David Gassner3h 9m Intermediate
1. Parallel Computing Hardware
2. Threads and Processes
3. Mutual Exclusion
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.