Method and Block Synchronization in Java
The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by sharing access and the object references.
In the Java programming language, there are two types of synchronization they are:
- Method Synchronization.
- Block Synchronization (Statement Synchronization).
The execution of the thread enters into synchronized block or method, then that block or method acquires lock until the task is completed.
The Synchronized methods run very slowly thus it reduces the performance of the program. It is recommended to use the synchronized method only when it is at most necessary. The Synchronization throw the null pointer exception when object is null in synchronized block.
Method Synchronization:
Method Synchronization is the process in that the object can able to visible to multiple threads. These threads can perform the read or write operations on the object. The Method Synchronization is used to overcome the problems of “thread interference” and “memory consistency” which raised by the normal communication between thread by the shared access to fields.
In the method synchronization, if one thread is invoked for execution in the synchronized method then other threads should wait until the initial thread completes its work.
Example program:
import java.io.*;
/*example program for accessing more than one thread to same object without synchronization*/
class Sync
{
public void getLine()
{
for(int i=0; i<3;i++)
{
System.out.println(i);
try
{
Thread.sleep(400);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
}
class Method extends Thread
{
/*creating reference for Sync object*/
Sync sy;
Method (Sync sy)
{
sy.getLine();
}
public void run()
{
sy.getLine();
}
}
class Main
{
public static void main(String args[])
{
/*object of the Sync class shared among threads*/
Sync ob = new Sync();
/*creating the threads that shares same object*/
Method m1 = new Method (ob);
Method m2 = new Method (ob);
/*Execution of the threads start*/
m1.start();
m2.start();
}
}
Output:
0
1
2
0
1
2
In the above program, the two thread are accessing the same object at the same time. So there is a chance of occurring the collision. To avoid the collision, the method synchronization is used
Example program:
import java.io.*;
class Sync
{
synchronized public void getLine()
{
for(int i=0; i<3;i++)
{
System.out.println(i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Method extends Thread
{
Sync sy;
Method (Sync sy)
{
this.sy= sy;
}
public void run()
{
sy.getLine();
}
}
class Main
{
public static void main (String args[])
{
/*object of the Sync class shared among threads*/
Sync ob = new Sync();
/*creating the threads that shares same object*/
Method m1 = new Method (ob);
Method m2 = new Method (ob);
/*Execution of the threads start*/
m1.start();
m2.start();
}
}
Output:
0
1
2
0
1
2
Block Synchronization:
The Block synchronization is the process of accessing the limited number of lines of code in a Method. In other words, the block synchronization performs on the specific resource in method by the shared access.
For Example, Let’s assume that a method contains 30 lines of code but the only 10 of them are modifiable. So, these specific 10 lines of code are synchronized, only one thread can access at a time to avoid collision and remaining lines of code can execute by the other threads.
Example Program:
import java.io.*;
import java.util.*;
class Block
{
String name=””;
public int count=0;
public void display(String str, List<String> list)
{
synchronized(this)
{
name = str;
count++;
}
list.add(str);
}
}
class Main
{
public static void main(String args[])
{
Block b = new Block();
List<String> list = new ArrayList<String>();
b.display(“Rahul”,list);
System.out.println(b.name);
}
}
Output:
Rahul
Advantages:
- By using the Synchronized methods, the multi-threading concept is achieved with shared resource.
- By using the Synchronized methods, the both instance methods and static methods (Synchronized) can able to executed simultaneously.
Disadvantages:
- The Synchronization methods decreases the efficiency because of the synchronized methods run slowly.
- The Synchronization doesn’t allow the read operation simultaneously.
Summary:
The Synchronization in Java is the process of synchronizing the specific methods or statement to avoid the collision when executing the threads concurrently. In java there are two types of synchronization they are “Method Synchronization” and “Block Synchronization”. The Synchronization is used to avoid the problems like thread interference and memory consistency errors.