educative.io

In Limiting philosophers about to eat section why is forks[(id + 1) % 5] used and not forks[(id + 4) % 5]

In Limiting philosophers about to eat section why does forks[(id + 1) % 5] used instead of forks[(id + 4) %5].

Previous to this section its mentioned that forks[id].acquire() represent left fork and
forks[(id + 4) %5] represent right fork. Failing to understand how does forks[(id + 1) % 5] represent right fork?

// acquire the left fork first
forks[id].acquire();
// acquire the right fork second
forks[(id + 4) % 5].acquire();


Course: Java Multithreading for Senior Engineering Interviews - Learn Interactively
Lesson: Dining Philosophers - Java Multithreading for Senior Engineering Interviews

Hi @Vilakshan,

You’re talking about the section “Limiting philosophers about to eat.” In this section, we write the code for only four philosophers’ who allow lifting/grabbing the forks. [forks[(id + 1) % 5]]` fulfils this condition. By doing so, only four philosophers picked the forks, and here we have one fork left that can be acquired by one of the philosophers to eat.