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.

Reentrant lock: Java demo

Reentrant lock: Java demo - Java Tutorial

From the course: Parallel and Concurrent Programming with Java 1

Start my 1-month free trial

Reentrant lock: Java demo

- [Instructor] In Java, there are three classes that implement the lock interface, a basic reentrant lock and two types of reentrant read write locks for reading and writing which we'll cover later. Notice that all three of those have the word reentrant in their name, and that's because Java does not have a standard non-reentrant lock. So for this video, I won't be able to compare a non-reentrant and a reentrant lock, so I'll just be using a reentrant lock to demonstrate its behavior. This is a variation of the previous example I used to demonstrate a data race and implementing mutual exclusion, with two shopper threads that concurrently increment the amount of items to buy on a shared shopping list. In this version, the shopper class has a private method on line 12 called addGarlic which uses the reentrant lock named Pencil to enforce mutual exclusion around a call to increment the class variable named Garlic on line 14. A related method on line 18 named addPotato performs a similar…

Contents