00:00

/* N58mBA - synchronized m/t */
/*
1. every object has unique lock
2. and object has two area sync. and non sync.
3. agar 2 thD ek saath ek exexute hoti hai aur hum chahte hai ki koi ek thD ke pahle
resource access kare to us thD ko uska lock acquare karna hoga.
4. is condition me pahla thD wait kareyga aur jaise hee dusra lock task coomplete
karega wo lock ko release kar deyga aur apna kaam karega.
5.
*/
class BookTheaterSeat{
int total_seats = 10;
synchronized void bookSeat(int seats){
if(total_seats>=seats){
System.out.println(seats+" seats booked successfully");
total_seats=total_seats-seats;
System.out.println("seats left = " + total_seats);
}
else{
System.out.println("sorry seats cannot be booked..!!");
System.out.println("seats left = "+total_seats);
}
}
}
public class N58mBA extends Thread{
static BookTheaterSeat b;
int seats;
public void run(){
b.bookSeat(seats);
}
public static void main(String[] args) {
b = new BookTheaterSeat();
N58mBA rajat = new N58mBA();
rajat.seats=7;
rajat.start();
N58mBA amit = new N58mBA();
amit.seats=6;
amit.start();
}
}
// both thread start simultaneously but one aquare lock of the object
/*
ramannegi@Ramans-MacBook-Pro x % java N58mBA
7 seats booked successfully
seats left = 3
sorry seats cannot be booked..!!
seats left = 3
*/