package theatre; public class Reservations { int totalSeats = 10; int reservedSeats = 8; public synchronized int checkFreeSeats(){ if(totalSeats - reservedSeats > 0){ //if there are free seats System.out.println("reserved = " + reservedSeats); return reservedSeats; //return the number of reserved seats }else{ return 0; } } //the integer represent the ith seat taken. if it is -1, the ticket was not bought. public synchronized int buyTicket() { //if there are free seats... if(checkFreeSeats() > 0){ //Simulate some processing time /* try { Thread.sleep(10000); } catch (InterruptedException e) { throw new RuntimeException(e); }*/ reservedSeats++; System.out.println("Updated to: " + reservedSeats); return reservedSeats; }else{ return -1; } } }