1

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.

  1. 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.

  2. 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?

  3. 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?

  4. 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...

Alex K
  • 22,315
  • 19
  • 108
  • 236
Foody
  • 15
  • 3
  • It would be nice if you posted those exceptions. – Sotirios Delimanolis Dec 24 '13 at 19:33
  • According stacktrace executorService are going to shutting down while you trying to submit new callable, so exception should be thrown. – aim Dec 24 '13 at 19:59
  • i don't understand why are they shutting down – Foody Dec 24 '13 at 20:28
  • because in some place `executorService.shutdown()` executed :) – aim Dec 24 '13 at 20:43
  • Check your code inside `Manager` class, after executing `RunnableChef.run` you are trying invoke this again, but `e.shutdown();` had already been invoked so this exception will be raised. For fix this, just remove shutdowning because if you creating executorService with `newCachedThreadPool` then threads in the pool will be automatically terminated after 60 secs. – aim Dec 24 '13 at 20:55
  • thank you! can you help me with this one? – Foody Dec 24 '13 at 21:02
  • at java.lang.Thread.run(Unknown Source) Exception in thread "pool-4-thread-1" java.util.ConcurrentModificationException at java.util.Vector$Itr.checkForComodification(Unknown Source) at java.util.Vector$Itr.next(Unknown Source) at RunnableStudent.takeOrder(RunnableChef.java:68) at RunnableStudent.run(RunnableChef.java:50) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) – Foody Dec 24 '13 at 21:04
  • You can't remove element inside loop while iterating at: `orders.removeElement(i);`, use instead `iterator.remove` for that, see [this for details](http://stackoverflow.com/questions/8104692/best-practice-to-avoid-java-util-concurrentmodificationexception-when-using-arra) – aim Dec 24 '13 at 21:09

0 Answers0