Java Blog

 
 
 
import java.io.*;

class customerOrder
{
    boolean valueset=false;
    String str[]=new String[3];

    synchronized void d_takeOrder(Thread t)
    {
        if(valueset)
        {
            try
            {
                wait();        
            }catch(InterruptedException e)
            {
                System.out.println(e);
            }
        }
        System.out.println(\"\\n\"+t);
        try
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            for(int i=0;i<3;i++)
            {
                System.out.print(\"\\n Take an Order \"+(i+1)+\" :: \");
                str[i]=br.readLine();
            }
        }catch(IOException e)
        {
            System.out.println(e);
        }

        valueset=true;
        notify();
    }
    synchronized void d_dispOrder(Thread t)
    {
        if(!valueset)
        {
            try
            {
                wait();        
            }catch(InterruptedException e)
            {
                System.out.println(e);
            }
        }
        System.out.println(\"\\n\"+t);
        for(int i=0;i<3;i++)
        {
            System.out.print(\"\\n Place an Order \"+(i+1)+\" :: \"+str[i]);
        }
        valueset=false;
        notify();
    }

}
class takeOrder implements Runnable
{
    customerOrder d;
    Thread t;
    takeOrder(customerOrder d)
    {
        this.d=d;
        t=new Thread(this,\"Manager take an order\");
        t.start();
    }
    public void run()
    {
        for(int i=0;i<2;i++)
        {
            d.d_takeOrder(t);
        }
    }
}
class dispOrder implements Runnable
{
    customerOrder d;
    Thread t;
    dispOrder(customerOrder d)
    {
        this.d=d;
        t=new Thread(this,\"Manager place an order\");
        t.start();
    }
    public void run()
    {
        for(int i=0;i<2;i++)
        {
            d.d_dispOrder(t);
        }
    }
}
class Restaurant
{
    public static void main(String args[])
    {
        customerOrder d=new customerOrder();
        new takeOrder(d);
        new dispOrder(d);
    }
}
/*
            Output

Thread[Manager take an order,5,main]

 Take an Order 1 :: 2 Roti

 Take an Order 2 :: 1 plat Veg.Jaipuri Sabji

 Take an Order 3 :: 1 plat Pulav

Thread[Manager place an order,5,main]

 Place an Order 1 :: 2 Roti
 Place an Order 2 :: 1 plat Veg.Jaipuri Sabji
 Place an Order 3 :: 1 plat Pulav

Thread[Manager take an order,5,main]

 Take an Order 1 :: 3 Roti

 Take an Order 2 :: 1 plat Paneerkadai Sabji

 Take an Order 3 :: 1 plat Biriyani

Thread[Manager place an order,5,main]

 Place an Order 1 :: 3 Roti
 Place an Order 2 :: 1 plat Paneerkadai Sabji
 Place an Order 3 :: 1 plat Biriyani

*/

 
 
Didn't find what you were looking for? Find more on Write a program for restaurant