Assume we have a restaurant which employs several chefs.
We also have a vector of incoming orders.
Those chefs receive orders and cook them until all the orders in the vector are cooked.
The class CallableCookWholeOrder is responsible for cooking the order. it returns the cooked one. The class RunnableChef simulates the chef. it can accept more than one order depending on the pressure factor. each order it accepts is sent to CallableCookWholeOrder. the class "management" is the one sending orders to the chefs.
If there is no available chef it should wait until a chef has done cooking some order and its pressure variable has decreased. in that case the chef notifies the management.
On the other hand, the chef must wait if he has no incoming orders at the moment or has finished cooking those he had, until a new order is delivered. in that case he must be notified and take care of that order.
how do i take care of the wait and notify in the management? I've tried creating an object and doing obj.wait() but it throws exceptions and i don't know what the problem is. i need for the management to wait in case that no one of the chefs is available.
i need an Executor in "RunnableChef" so i could submit the orders returned from the "CallableCookWholeOrder",i use the newCachedThreadPool() executor so it will have a dynamic number of threads because this number differs from one chef to another and his pressure factor. is it right to use this type in my case? are there other suggestions?
in "RunnableChef" i have a vector of incoming orders. i remove an order once it's taken care of so that an empty vector indicates that a chef should wait. is it a right thing to do?
those are the methods in "RunnableChef":
public synchronized Boolean canAcceptTask(Order order){
if(...) return true;
return false;
}
public synchronized void accept(Order order){
this.pressure = this.pressure+order.getDifficulty();
orders.add(order);
this.notifyAll();
}
public void run() {
this.takeOrder();
e.shutdown();
}
private synchronized void takeOrder(){
while(orders.isEmpty()){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(Order order:orders) {
Future<Order> cooked = e.submit(new CallableCookWholeOrder(order));
allCooked.add(cooked);
this.pressure = this.pressure - order.getDifficulty();
orders.removeElement(i);
this.check.update(); //notifies the management
}
}
I'm getting exceptions on the submit line and the takeOrder line.
Exception in thread "pool-4-thread-4" java.util.concurrent.RejectedExecutionException:
Task java.util.concurrent.FutureTask@50a5314 rejected from java.util.concurrent.ThreadPoolExecutor@24065c4[Shutting down, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.reject(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at RunnableChef.takeOrder(RunnableChef.java:73)
at RunnableChef.run(RunnableChef.java:50)
and more of a kind...