I have 3 classes. A Waiter (thread), a Table (thread), and a Queue (not a thread) (simiple first in first out queue).
Initially a couple of waiters are created and then tables are created. There are many more tables than waiters so tables have to “wait” for waiters to serve them. I am supposed to use “java synchronization primitives, wait notify, and notifall” to solve this problem. Here are the guidelines for each class:
Table:
When it is started, it enters the queue and then calls an internal method waitService() which returns if it has been serviced or waits for a waiter.
the way I have this method right now:
$if(serviced)
return 0;
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Waiter:
The waiter code is not really giving me any issues but essentially the way it works is that in the run method there is an infinite loop that checks if there is a table waiting and then gives that table service for a random amount of time after which it checks if another table is waiting. It lets the table know it is giving it service by calling giveService() method inside the table class.
Queue:
It has a list of waiters and a list of tables. The waiter calls nextTable() in the queue class when it is ready to give service. this is how I have it set up:
$public Table nextTable(Waiter w) throws InterruptedException {
if(closed)
return null;
if(tables.isEmpty()) {
w.wait();
}
Table t = tables.removeFirst();
t.notify();
return t;
}
I know the above may have some very fundamental mistakes but essentially I have no clue how synchronization primitives work in java. Any help would be appreciated.
This is as good of a tutorial as any on using wait(), notify() and notifyAll(). Read through it first, and see if you have additional questions that either SO hasn’t already answered, or something in particular to your problem.
http://www.java-samples.com/showtutorial.php?tutorialid=306