I'm newbie in programming so I decided to write a simple multithreading program. It shows the work of the restaurant.Client orders food,waiter serves and cook prepares it. But I've got a problem, I think this is the case of deadlocking because when I run it, it prints "ordering" and nothing else. I can't understand what is wrong. Please help.Thanks.
Restaurant.java
public class Restaurant implements Runnable{
Client cl=new Client();
Chef ch=new Chef();
Waiter w=new Waiter();
public synchronized void makeOrder() throws InterruptedException{
notifyAll();
cl.makeOrder();
wait();
}
public synchronized void makeServing() throws InterruptedException{
notifyAll();
wait();
}
public synchronized void makeFood() throws InterruptedException{
notifyAll();
ch.makeFood();
Thread.sleep(1000);
wait();
}
@Override
public void run() {
try {
for(int i=0;i<10;i++){
makeOrder();
makeServing();
makeFood();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client.java
public class Client{
public void makeOrder(){
System.out.println("Ordering"+Thread.currentThread().getId());
}
Waiter.java
public class Waiter {
public void makeServe() {
System.out.println("Serving order"+Thread.currentThread().getId());
}
Chef.java
public class Chef {
public void makeFood(){
System.out.println("Making food "+Thread.currentThread().getId());
}
Main.java
public class Main {
public static void main(String[] args) {
Restaurant r=new Restaurant();
Thread t=new Thread(r);
t.start();
}
}