Professional Documents
Culture Documents
Exp 3 Os
Exp 3 Os
Synchronous:
Code:
class Bus{
int ts,rs;
Bus(int ts){
this.ts=ts;
rs=ts;
}
synchronized void bookTicket(String user){
try{
if(rs>0){
Thread.sleep(1000);
rs--;
System.out.println("Seats for "+user+"is booked!!!");
System.out.println("Seats left:"+rs);
}
else{
System.out.println("Seats are full!!!");
}
}
catch(Exception e){}
}
}
Asynchronous:
Code:
class Bus{
int ts,rs;
Bus(int ts){
this.ts=ts;
rs=ts;
}
void bookTicket(String user){
try{
if(rs>0){
Thread.sleep(1000);
rs--;
System.out.println("Seats for "+user+"is booked!!!");
System.out.println("Seats left:"+rs);
}
else{
System.out.println("Seats are full!!!");
}
}
catch(Exception e){}
}
}
Output: