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.
Deadlock: Java demo - Java Tutorial
From the course: Parallel and Concurrent Programming with Java 1
Deadlock: Java demo
- [Instructor] To demonstrate a Deadlock with the Dining Philosopher's problem and how to solve it, I'm going to expand our scenario from having just two philosophers, Olivia and me, to having three philosophers, Olivia, me, and our buddy Steve, and we're each competing for two of the three chopsticks placed around the table, labeled here as A, B, and C. In this example Java program, I've defined a Philosopher class on line seven to represent a philosopher who thinks and eats sushi. The philosopher has two instance variables on line nine named firstChopstick and secondChopstick, which are the two locks it will acquire in that order as well as a static class variable on line 10 to represent the amount of sushi left between all of the philosophers. When I instantiate a philosopher, I'll pass a pair of locks to the constructor method defined on line 12, which will assign them to the first and second chopstick positions. Down in the philosopher's run method, the while loop on line 19 will…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.