Synchronization and locks:
The access to shared resources is not just a problem for Java nor even for object oriented languages in general. It is a potential problem for all systems where shared data is being used. The solution to this particular problem adopted by Java and a number of other programming languages is to designate certain methods that access shared data as synchronized. This is achieved by prefacing the method with the Java keyword synchronized (note the spelling of this word).
Each object in Java is said to have a lock. A thread entering a synchronized method gains the lock on the object that contains the method and no other thread can access any synchronized method in that object until the lock is released - in this case, by the thread completing the processing inside the method.
We need to ensure that all methods that access the shared data are modified by the keyword synchronized because non-synchronized methods can still have access to the shared data if they are invoked. Then, when a thread enters the code of one of the synchronized methods, no other thread can interrupt its processing to gain access to the shared data through synchronized methods: the thread that has entered completes the processing of that method. This is the case even if that particular thread is blocked and, as we shall see in a later section, this in itself can cause significant problems. It is the responsibility of the programmer to ensure that all of the necessary methods are declared as synchronized.
During the actual processing, there may be a number of threads trying to access an object. What happens to them. The answer is that the Java system maintains, for each created object that is associated with a class that has synchronized methods, a set of threads awaiting permission to access this object - although again, which thread is selected next is arbitrary and not based on any sort of order.
In our particular example program, the problem lies in the NumberOfBookedSeats class and, in particular, the methods that access the data field total. What we want to be able to do is say that once a thread has entered one of these methods, it will have sole access until it has finished. We achieve this using the keyword synchronized.
Below is how we need to modify our program - we simply need to declare the three methods as synchronized.
public class NumberOfBookedSeats
{
private int total;
private int newSum;
public NumberOfBookedSeats ()
{
total = 0;
}
// now declared synchronized
public synchronized void addOne ()
{
newSum = getTotal() + 1;
try
{
Thread.sleep(2);
}
catch (InterruptedException e)
{
String errMessage = e.getMessage();
System.out.println("Error " + errMessage);
}
setTotal(newSum);
}
public synchronized int getTotal ()
{
return total;
}
public synchronized void setTotal (int value)
{
total = value;
}
}
This small change ensures that once a thread has gained the lock on NumberOfBookedSeats, no other thread can do so until the first thread has released it by leaving the method. When we run the program now, we get a final value in NumberOfBookedSeats of 100, as we had originally expected.
The general picture on locks is that, in Java, every object contains a lock - this is part of the class Object from which ultimately all objects inherit and it does not require you to do any coding to create it. Most of the time you don't make use of this facility but when dealing with threads the lock becomes a valuable tool. As there is only one lock per object then, even if you have several synchronized methods in an object, once a thread has invoked one of them, all other threads are blocked and no thread can access the other synchronized methods until the first one releases the lock. Methods that are not synchronized are not affected by the locking mechanism.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Synchronization and locks homework and assignments? Live Synchronization and locks experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Synchronization and locks homework help, java assignment help and Synchronization and locks 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