Master java skills

Synchronized Block in java

Synchronized methods synchronize entire method. But sometimes, this is not what we want. A method may be 50 or 100 lines long, but only 3 lines out of them are required to be synchronized. In such a case, synchronizing the entire method will result in slowness of the system. Therefore, java provides a way known as ‘synchronized block’.

In synchronized block, we can synchronize only a block of code. This block can be as small as a single line of code to as big as n number of lines. This is a better solution than synchronized method.

Syntax

synchronized( lockObject ) {
     //some code
}

Synchronized block example

package com.javatrainingschool;

public class Number {

	public void displayNumbers() {

		System.out.println(Thread.currentThread().getName() + " : Entering displayNumbers method");
		System.out.println(Thread.currentThread().getName() + " : This is the second statement");

		synchronized (this) {
			for (int i = 0; i < 5; i++)
				System.out.println(Thread.currentThread().getName() + " : " + i);
		}
		System.out.println(Thread.currentThread().getName() + " : Existing displayNumbers method");
	}

}
package com.javatrainingschool;
public class SynchronizedMethodExample implements Runnable {
	
	Number number;
	
	public SynchronizedMethodExample(Number number) {
		this.number = number;
	}
	
	@Override
	public void run() {
		number.displayNumbers();
	}
	
	public static void main(String[] args) {
		
		Number number = new Number();
		
		Runnable rt1 = new SynchronizedMethodExample(number);
		Runnable rt2 = new SynchronizedMethodExample(number);
		Runnable rt3 = new SynchronizedMethodExample(number);
		
		Thread t1 = new Thread(rt1);
		Thread t2 = new Thread(rt2);
		Thread t3 = new Thread(rt3);
		
		t1.start();
		t2.start();
		t3.start();
	}
}

In the below output, you can observe that only those statements are synchronized which are inside the synchronized block.

Output :

Thread-1 : Entering displayNumbers method
Thread-0 : Entering displayNumbers method
Thread-0 : This is the second statement
Thread-2 : Entering displayNumbers method
Thread-0 : 0
Thread-2 : This is the second statement
Thread-0 : 1
Thread-1 : This is the second statement
Thread-0 : 2
Thread-0 : Existing displayNumbers method
Thread-2 : 0
Thread-2 : 1
Thread-2 : 2
Thread-2 : Existing displayNumbers method
Thread-1 : 0
Thread-1 : 1
Thread-1 : 2
Thread-1 : Existing displayNumbers method

Example 2

package com.javatrainingschool;

public class Calculation {

	public void sum(int i) {
		synchronized(this) {  //this represents the object that invokes this method
			System.out.println(Thread.currentThread().getName() + " : sum = " + (i + i));	
		}
	}
	
	public void square(int j) {
		synchronized(this) {
			System.out.println(Thread.currentThread().getName() + " : square = " + j*j);	
		}
	}
}
package com.javatrainingschool;

public class SynchronizedBlockExample implements Runnable {

	Calculation calculation;
	
	public SynchronizedBlockExample(Calculation calculation) {
		super();
		this.calculation = calculation;
	}
	
	@Override
	public void run() {
		calculation.sum(4);
		calculation.square(6);
	}
	
	public static void main(String[] args) {
		Calculation calculation = new Calculation();
		Runnable rt1 = new SynchronizedBlockExample(calculation);
		Runnable rt2 = new SynchronizedBlockExample(calculation);
		Runnable rt3 = new SynchronizedBlockExample(calculation);
		Thread t1 = new Thread(rt1, "thread-a");
		Thread t2 = new Thread(rt2, "thread-b");
		Thread t3 = new Thread(rt3, "thread-c");
		
		t1.start();
		t2.start();
		t3.start();
			
	}
}
Output :
thread-a : sum = 8
thread-a : square = 36
thread-c : sum = 8
thread-c : square = 36
thread-b : sum = 8
thread-b : square = 36