import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class DiningServerImpl implements DiningServer {
// different philosopher states
enum State {
THINKING, HUNGRY, EATING
};
// number of philosophers (a constant)
public static final int numOfPhils = 5;
// array to record each philosopher's state
State[] states = new State[numOfPhils];
Condition[] self = new Condition[numOfPhils];
// synchronization variables (Lock and Condition)
Lock lock;
public DiningServerImpl() {
// array of philosopher's state
// originally all philosopher's are thinking
for (int i = 0; i < numOfPhils; i++) {
states[i] = State.THINKING;
}
lock = new ReentrantLock();
for (int i = 0; i < numOfPhils; i++)
self[i] = lock.newCondition();
}
// the method called by a philosopher when they wish to eat
public void takeForks(int philNum) {
states[philNum] = State.HUNGRY;
test(philNum);
if (states[philNum] != State.EATING) {
System.out.println(philNum + " CAN'T EAT OMG");
while ( states[philNum] != State.EATING) {
try { self[philNum].wait(); }
catch(InterruptedException e) {}
}
}
}
// the method called by a philosopher when they are finished eating
public void returnForks(int philNum) {
states[philNum] = State.THINKING;
test((philNum + 4) % 5);
test((philNum + 1) % 5);
lock.unlock();
}
private void test(int i) {
if ((states[(i + 4) % 5] != State.EATING)
&& (states[i] == State.HUNGRY)
&& (states[(i + 1) % 5]) != State.EATING) {
states[i] = State.EATING;
self[i].signal();
}
}
}
and I'm getting an error when using self[i].signal and I'm not sure why. Can anyone help? Here is a sample output.
philosopher 0 is thinking. philosopher 3 is thinking. philosopher 2 is thinking. philosopher 1 is thinking. philosopher 4 is thinking. philosopher 2 is hungry. philosopher 1 is hungry. philosopher 4 is hungry. 1 CAN'T EAT OMG Exception in thread "Thread-2" Exception in thread "Thread-4" Exception in thread "Thread-1" java.lang.IllegalMonitorStateException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signal(Unknown Source) at DiningServerImpl.test(DiningServerImpl.java:61) at DiningServerImpl.takeForks(DiningServerImpl.java:37) at Philosopher.run(Philosopher.java:26) at java.lang.Thread.run(Unknown Source) java.lang.IllegalMonitorStateException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:503) at DiningServerImpl.takeForks(DiningServerImpl.java:41) at Philosopher.run(Philosopher.java:26) at java.lang.Thread.run(Unknown Source) java.lang.IllegalMonitorStateException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signal(Unknown Source) at DiningServerImpl.test(DiningServerImpl.java:61) at DiningServerImpl.takeForks(DiningServerImpl.java:37) at Philosopher.run(Philosopher.java:26) at java.lang.Thread.run(Unknown Source) philosopher 0 is hungry. 0 CAN'T EAT OMG Exception in thread "Thread-0" java.lang.IllegalMonitorStateException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:503) at DiningServerImpl.takeForks(DiningServerImpl.java:41) at Philosopher.run(Philosopher.java:26) at java.lang.Thread.run(Unknown Source)

New Topic/Question
Reply


MultiQuote

|