Accessing shared resources:
The problem of accessing shared resources occurs in the overwhelming majority of real life situations, where there is ultimately one data object being shared by many computers or threads. For example, an airline booking system must, somewhere, have a single record of how many seats on a plane have been booked. Yet that single record must be available to hundreds, if not thousands, of computers in travel agent offices around the world who want to check availability and then book seats for their clients. This is a problem that has always existed for recording systems - even paper-based systems can suffer from this problem.
Consider the following three classes:
AirlineBookingTester
NumberOfBookedSeats
TravelAgent
The first class, AirlineBookingTester, is quite simple and consists of a main method. In the main method, we create an instance of a class called NumberOfBookedSeats. We also create 100 instances of a class called TravelAgent which, from the use of the start method, we can see is a threaded class. The main method then sleeps for a second to allow all of the threads time to complete their processing before printing out the total in amount.
public class AirlineBookingTester
{
public static void main (String [] args)
{
NumberOfBookedSeats amount = new NumberOfBookedSeats();
for (int count = 0; count < 100; count++)
{
TravelAgent thisone = new TravelAgent(amount);
thisone.start();
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
String errMessage = e.getMessage();
System.out.println("Error " + errMessage);
}
System.out.println("end total = " + amount.getTotal());
}
}
The NumberOfBookedSeats class is given below. As well as a constructor that initializes total to zero, it consists of three methods - getter and setter methods, which handle access to total, and a method called addOne, which gets the value of total, adds 1 to it, sleeps for a short while to simulate some sort of complex processing or I/O delay, and then uses the set method to update the value of total.
public class NumberOfBookedSeats
{
private int total;
private int newSum;
public NumberOfBookedSeats ()
{
total = 0;
}
public void addOne ()
{
newSum = getTotal() + 1;
try
{
Thread.sleep(2);
}
catch (InterruptedException e)
{
String errMessage = e.getMessage();
System.out.println("Error " + errMessage);
}
setTotal(newSum);
}
public int getTotal ()
{
return total;
}
public void setTotal (int value)
{
total = value;
}
}
Finally, we have the TravelAgent class, which extends Thread. On its creation, it expects to be passed an instance of NumberOfBookedSeats. The run method is overridden and it invokes the addOne method of the NumberOfBookedSeats instance.
public class TravelAgent extends Thread
{
private NumberOfBookedSeats sum;
public TravelAgent (NumberOfBookedSeats number)
{
sum = number;
}
public void run ()
{
sum.addOne();
}
}
This sounds fairly straightforward. We create 100 threads, each of which calls a method in an object that adds 1 to a total. Therefore, we would expect the println statement in the main method at the end to report that end total = 100.
However, when we actually run the code this is not what happens. Instead, the total value typically comes out at between 10 and 23 - it varies with each run. So what is going on herefi Not only do we not get the answer expected, but the answer that we get varies with each run!
The problem is the use of a shared data object - in this case, it is the single instance of the NumberOfBookedSeats class.
So what is wrong with the programfi The problem lies in the addOne method of NumberOfBookedSeats. Each of the threads invokes this method when they are set running. When the addOne method is invoked by a particular thread, the following three things happen:
- the getTotal method is invoked to fetch the current value of total and add 1;
- the thread then sleeps to simulate some I/O processing;
- the setTotal method is invoked to update total to the new value.
You can imagine that this is like a travel agent first requesting how many seats have been taken on a plane, next adding one person to the number, then having to send the update via a dial-up link and finally the main computer updating its records to show this new addition.
Problems arise because each thread has to share the time with the CPU and so threads are continually being switched between running, runnable and blocked.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Accessing shared resources homework and assignments? Live Accessing shared resources experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Accessing shared resources homework help, java assignment help and Accessing shared resources projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours