Chopstick implementation
Chopstick implementation
class Chopstick {
boolean taken=false;
synchronized void put() {
taken=false;
notify();
}
synchronized void get()
throws InterruptedException
{
while (taken) wait();
taken=true;
}
}
CHOP is implemented by the monitor class Chopstick. The choice in CHOP is provided by the get() operation which allocates the chopstick to the “first” philosopher to request it and blocks a neighbours request until the fork is released by put(). The notion of “first” is realised by the synchronization lock which ensures that only one thread can be executing in get() at any one time.