Method Level Synchronization

package multithreading;
public class CustomerBill {
public static void main(String[] args){
McDonald m = new McDonald();
Customer1 c1 = new Customer1(m,”kundan”,”Burger and French_Fries”,125.50f);
Customer2 c2 = new Customer2(m,”Samarjeet”,”Aloo Mc_Tikki,Mc_Vegi”,375);

c1.start();
c2.start();
}
}

class McDonald{
String itemName;
float price;

public synchronized void Menu(String name,String itemName, float price){
this.itemName = itemName;
this.price = price;
System.out.println(“Customer :”+name);

System.out.println(“Item :”+itemName+”t” + “Price :”+price);

try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}

}
}

class Customer1 extends Thread{
McDonald m;
String name;
String itemName;
float price;

Customer1(McDonald m,String name,String itemName, float price){
this.m = m;
this.name = name;
this.price = price;
this.itemName = itemName;
}
public void run(){
m.Menu(name,itemName,price);
}
}

class Customer2 extends Thread{
McDonald m;
String name;
String itemName;
float price;

Customer2(McDonald m,String name,String itemName,float price){
this.m = m;
this.name = name;
this.price = price;
this.itemName = itemName;
}
public void run(){
m.Menu(name,itemName,price);

}
}

One thought on “Method Level Synchronization”

Leave a Reply