Java Synchronization

Synchronization is a concept which controls or restricts the access of a shared resource or any data/member of an object can be accessed one thread at a time. If multiple threads are trying to access a single object, there might be heavy chances of the Object values gets overridden or corrupted without synchronization.

In these conditions synchronization is helpful to regulate the accessing of a shared resource or an object by multiple threads. Synchronization helps to allow only one thread at a time to access the shared resource, if the synchronization is implemented properly.

In this example lets try to print the numbers from 1 to 10 by sharing the Integer class object using two threads.

public class ReadAndIncrementInteger extends Thread {

	public  IntegerClass integerClass;

	public static void main(String[] args) {
		IntegerClass integerClass=new IntegerClass();

		Thread thread1 = new ReadAndIncrementInteger(integerClass);
		Thread thread2 = new ReadAndIncrementInteger(integerClass);
		
		thread1.start();
		thread2.start();
	}

	public ReadAndIncrementInteger(IntegerClass integerClass) {
		this.integerClass = integerClass;
	}

	public void run() {
		
		
			while(this.integerClass.getInteger()<10)
			{
				
				this.integerClass.setInteger(this.integerClass.getInteger()+1);
				System.out.println("Integer Value  : "+this.integerClass.getInteger());
				}
			
	}

}


 class IntegerClass {
	 
	 public Integer integer=new Integer(0);

	public Integer getInteger() {
		return integer;
	}

	public void setInteger(Integer integer) {
		this.integer = integer;
	}
}

From the above example, we see the number should print from 1 to 10 but the output came as the following.

Output
Integer Value  : 2
Integer Value  : 3
Integer Value  : 4
Integer Value  : 5
Integer Value  : 6
Integer Value  : 7
Integer Value  : 8
Integer Value  : 9
Integer Value  : 10
Integer Value  : 2

If we see the above output, the numbers are not printing in the sequential order instead of sequential order its printing like a random numbers(Output may vary from processor to processor). This is one of the classical example of thread sync issues.

Root Cause of this issue is we are using a shared resource or Object using across two threads caused this issue, in our example Integer Class Object is a shared resource. Solution for this problem is nothing but a synchronization.

Synchronization implementation In Java

Object Level Lock Example

Lets add the Synchronization block to the above output and see the output how it works.

public class ReadAndIncrementInteger extends Thread {

	public IntegerClass integerClass;

	public static void main(String[] args) {
		IntegerClass integerClass = new IntegerClass();

		Thread thread1 = new ReadAndIncrementInteger(integerClass);
		Thread thread2 = new ReadAndIncrementInteger(integerClass);

		thread1.start();
		thread2.start();
	}

	public ReadAndIncrementInteger(IntegerClass integerClass) {
		this.integerClass = integerClass;
	}

	public void run() {

		synchronized (this.integerClass) {

			while (this.integerClass.getInteger() < 10) {

				this.integerClass.setInteger(this.integerClass.getInteger() + 1);
				System.out.println("Integer Value  : " + this.integerClass.getInteger());
			}
		}
	}

}

class IntegerClass {

	public Integer integer = new Integer(0);

	public Integer getInteger() {
		return integer;
	}

	public void setInteger(Integer integer) {
		this.integer = integer;
	}

}
Output
Integer Value  : 1
Integer Value  : 2
Integer Value  : 3
Integer Value  : 4
Integer Value  : 5
Integer Value  : 6
Integer Value  : 7
Integer Value  : 8
Integer Value  : 9
Integer Value  : 10