Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:39:39+00:00 2026-06-15T11:39:39+00:00

I am new to Java and was trying to understand how synchronizatioon works. So

  • 0

I am new to Java and was trying to understand how synchronizatioon works. So I created a Plane Reservation System and I am able to simulate mutliple users trying to make a reservation and use Synchromization to get the correct output.

Now that it works, I am thinking how this works in the real world. For instance, let us say the application is built on Swing and this is a multi user application. For simplicity let us assume there are only two planes ‘AAA’ and ‘BBB’. This application may be installed in each of the ticket counter agent’s computer , the kiosks at the airport and as well as in the computers of different travel agents and all of them accessing the same database.

In this case, each user/computer will have its own instance of Reserve class, Transaction class and Plane class. So there is only one thread/request in the Transaction class and there is no synchronization.

My question is, how will a mutli user application like this Reservation system actually be designed/implemeneted such that all users are accessing one instance of Transacion class so that synchronization can happen. You can also look at this question as, how can I build a mutli-user game played by different players across different computers. One more example would be a Banking system to make deposits, withdrawals and transfers when the application is running in the ATM as well as the Teller’s machine.

///////////////////////////////

Reserve.java —> Point of entry for each request made by a user

/////////////////////////////

import java.io.IOException;

public class Reserve {

static int queryseatsavailableinx;
static int queryseatsavailableiny;

static
{
    seats s = null;
    try {
        s = new seats();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        queryseatsavailableinx = s.getseatsinplanex("AAA");
        queryseatsavailableiny = s.getseatsinplanex("BBB");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

static final Plane x1 = new Plane("AAA", 001, queryseatsavailableinx );
static final Plane y1 = new Plane("BBB", 002, queryseatsavailableiny);
static final Transaction trans = new Transaction();

public static void main(String[] args) throws InterruptedException {

    Thread t1 = new Thread(new Runnable() {

        public void run() {
            trans.getPlaneInfo(x1);
        }
    });

    Thread t2 = new Thread(new Runnable() {

        public void run() {
            //trans.getPlaneInfo(x1);
            trans.reserveSeats(x1,3);
        }
    });

    Thread t3 = new Thread(new Runnable() {

        public void run() {
            //trans.getPlaneInfo(y1);
            trans.reserveSeats(y1,8);
        }
    });

    Thread t4 = new Thread(new Runnable() {

        public void run() {
            //trans.getPlaneInfo(x1);
            trans.reserveSeats(x1,2);
        }
    });

    t1.start();
    t2.start();
    t3.start();
    t4.start();

    t1.join();
    t2.join();
    t3.join();
    t4.join();
 }

}

///////////////////////////////

Transaction.java —> Actual transaction happens here

/////////////////////////////

public class Transaction {

public void getPlaneInfo(Plane x){
    synchronized(this){
        int number =  x.getSeatCapacity();
        String planename=x.getPlaneName();
        System.out.printf("The number of seats in plane %s is %d\n",planename,number);
    }

}

public void reserveSeats(Plane x, int seatstobereserved) {
    synchronized(this){
    x.updateSeatCapacity(seatstobereserved);
    }
  }
 }

///////////////////////////////

Plane.java —> Information about the plane

/////////////////////////////

final public class Plane {

private String planename = null;
private int planeid = 0;
private int availableseatcapacity = 0;

Plane(String planename, int planeid, int seatcapacity) {
    this.planename = planename;
    this.planeid = planeid;
    this.availableseatcapacity = seatcapacity;
}

public String getPlaneName() {
    return planename;
}

public int getPlaneId() {
    return planeid;
}

public int getSeatCapacity() {
    return availableseatcapacity;
}

public void updateSeatCapacity(int reservedseats) {
    availableseatcapacity -= reservedseats;
    System.out.printf("\n%d Seats successfully reserved and remaining seats " +
            "in the plane %s are %d\n",reservedseats, planename,availableseatcapacity);

 }

}

///////////////////////////////

Seats.java —> To simulate each time a request is made by the user the ‘number of availabe seats’ is retrieved from a common datasource/db.
/////////////////////////////

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

final public class seats {

seats() throws IOException {
    DataOutputStream di1 = new DataOutputStream(new FileOutputStream(
            "\\PlaneReservation\\bin\\a.bin"));
    DataOutputStream di2 = new DataOutputStream(new FileOutputStream(
            "\\PlaneReservation\\bin\\b.bin"));

    di1.writeInt(300);
    di2.writeInt(200);
    di1.flush();
    di2.flush();
    di1.close();
    di2.close();
}

public int getseatsinplanex(String s) throws IOException {
    if (s.equals("AAA")) {
        FileInputStream fis1 = new FileInputStream("\\PlaneReservation\\bin\\a.bin");
        DataInputStream dis1 = new DataInputStream(fis1);
        int number = 0;
        boolean eof = false;
        while (!eof) {
            try {
                number = dis1.readInt();
            //  System.out.println(number);
            } catch (EOFException eofx) {
                eof = true;
                dis1.close();
            }
        }
        return number;
    } else if (s.equals("BBB")) {
        FileInputStream fis2 = new FileInputStream("\\PlaneReservation\\bin\\b.bin");
        DataInputStream dis2 = new DataInputStream(fis2);
        int number = 0;
        boolean eof = false;
        while (!eof) {
            try {
                number = dis2.readInt();
                //System.out.println(number);
            } catch (EOFException eofx) {
                eof = true;
                dis2.close();
            }
        }
        return number;
    }
    return 0;
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:39:40+00:00Added an answer on June 15, 2026 at 11:39 am

    Symplistically, you’d make all Transactions methods synchronized: this would give you mutual exclusion.

    In reality, however, no system is designed like that. The server-side application is not modelled as a single synchronized object. Rather, the durable state is maintained in a relational database that supports ACID transactions (atomic, concurrent, isolated, and durable) and the equivalent of the Transaction object is a stateless singleton object that needs no synchronization on Java level. Such an object would be called a service bean. It would typically be created within a Dependency Injection container, such as Spring, and it would be connected to a bunch of other objects, such as DAOs (Data Access Object), which would in turn implement the low-level logic of interaction with a database. The Dependency Injection container makes it easy to declaratively build a complex graph of interconnected objects. A typical enterprise application, such as an airline’s reservation system, contains at least tens of service beans and even more DAOs, which connect to a variety of back-end systems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

new to Java programming, I am just trying to understand how I can make
I'm new to Java and trying to understand how dictionary in compression works. public
I am new to mutli-threading in java and trying to understand the keyword synchronization
I'm relatively new to Java and I'm still trying to understand the fundamentals. I
I'm still new to programming/Java/Android so I'm trying to understand everything I do and
I am relatively new java. I am trying understand what are the usage of
I'm trying to understand how the new try-with-resources statement works by recreating it using
I am trying to understand threads in Java. As an exercise, I created an
I am new to java programming and am trying to understand the correct way
I'm new to Android(and Java also) and I'm trying to understand what are Fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.