CoreJava

Lets see Example of Multithreading using Bus Ticket Reservation System.

We have one class Bus Reservation which implements Runnable interface.It has run() method which calls another method name bookTickets(-,-). bookTickets(-,-) method has code to check and book the ticket.

1.BusReservation

package Bus;
public class BusReservation implements Runnable{
private int totalSeatsAvailable=2;
public void run() {
//BusReservation br = new BusReservation();
PassengerThread pt = (PassengerThread) Thread.currentThread();
boolean ticketsBooked = this.bookTickets(pt.getSeatsNeeded(), pt.getName());
if(ticketsBooked==true){
 System.out.println("CONGRATS"+Thread.currentThread().getName()+" .. The number of                                                                    seats requested("+pt.getSeatsNeeded()+")  are BOOKED");
}
                   else{
 System.out.println("Sorry Mr."+Thread.currentThread().getName()+" .. The number                                                             of seats requested("+pt.getSeatsNeeded()+")  are not available");
}
}

  public  synchronized boolean bookTickets(int seats, String name){
 System.out.println("Welcome to HappyBus "+Thread.currentThread().getName()+" Number of                                                                          Tickets Available are:"+this.getTotalSeatsAvailable());

if (seats>this.getTotalSeatsAvailable()) {
return false;
} else {

totalSeatsAvailable = totalSeatsAvailable-seats;

return true;
}
}
private int getTotalSeatsAvailable() {
return totalSeatsAvailable;
}

}
                                --------------------------------------------------
2.PassengerThread 
Here  we have Passenger Thread which acts as thread and take no seats as argument.

package Bus;
public class PassengerThread extends Thread{
private int seatsNeeded;

public PassengerThread(int seats, Runnable target, String name) {
super(target,name);
this.seatsNeeded = seats;
}

public int getSeatsNeeded() {
return seatsNeeded;
}
}
                               ---------------------------------------------
3.RedBus

Here we have test file.

package Bus;
public class RedBus{

public static void main(String[] args){
BusReservation br = new BusReservation();
PassengerThread pt1 = new PassengerThread(2,br,"SAM");
PassengerThread pt2 = new PassengerThread(2, br, "Jack");
pt1.start();
pt2.start();
}
}

the PassengerThread takes(noOfSeats,Object and name of Passenger as a argument).
after we are starting Thread.






any queries please comment.Thanks




   

No comments:

Post a Comment