0% found this document useful (0 votes)
12 views3 pages

Exp 3 Os

The document contains code to demonstrate synchronous and asynchronous threading in Java. For the synchronous example, the bookTicket method in the Bus class is synchronized, preventing multiple threads from accessing it simultaneously. For the asynchronous example, the bookTicket method is not synchronized, allowing multiple threads to access it concurrently and potentially producing unexpected output. The main method creates Bus and Passenger objects and starts the passenger threads to call bookTicket, outputting the results.

Uploaded by

Taran Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Exp 3 Os

The document contains code to demonstrate synchronous and asynchronous threading in Java. For the synchronous example, the bookTicket method in the Bus class is synchronized, preventing multiple threads from accessing it simultaneously. For the asynchronous example, the bookTicket method is not synchronized, allowing multiple threads to access it concurrently and potentially producing unexpected output. The main method creates Bus and Passenger objects and starts the passenger threads to call bookTicket, outputting the results.

Uploaded by

Taran Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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){}
}
}

class Passenger extends Thread{


Bus b;
String name;
Passenger(Bus b,String name){
this.b=b;
this.name=name;
}
public void run(){
b.bookTicket(this.name);
}
}
class Syncc{
public static void main(String args[]){
Bus myBus=new Bus(3);
System.out.println("Taran Shah C164 C3-2 60004220256");
Passenger user1=new Passenger(myBus,"user1");
Passenger user2=new Passenger(myBus,"user2");
user1.start();
user2.start();
}
}
Output:

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){}
}
}

class Passenger extends Thread{


Bus b;
String name;
Passenger(Bus b,String name){
this.b=b;
this.name=name;
}
public void run(){
b.bookTicket(this.name);
}
}
public class Asyncc{
public static void main(String args[]){
Bus myBus=new Bus(3);
System.out.println("Taran Shah C164 C3-2 60004220256");
Passenger user1=new Passenger(myBus,"user1");
Passenger user2=new Passenger(myBus,"user2");
user1.start();
user2.start();
}
}

Output:

You might also like