What is Synchronization in Java
In this article, We are going to learn what is synchronization in java with example one-by-one in easy way so that you can understand well.
Java synchronization concept allows only one thread at a time to access shared resources i.e multiple thread can access shared resources one-by-one.
In other words you can say, Multiple thread cannot access shared resource at the same if there is synchronization concept in our java program.
Synchronization is also known as Thread-Safe i.e only one thread can access any shared resource at a time.
Java synchronization concept allows only one thread at a time to access shared resources i.e multiple thread can access shared resources one-by-one.
In other words you can say, Multiple thread cannot access shared resource at the same if there is synchronization concept in our java program.
Synchronization is also known as Thread-Safe i.e only one thread can access any shared resource at a time.
What is the use of synchronization in multithreading?
There are many usage of synchronization in multithreading.
- To prevent concurrent access to a block of code or object by multiple thread.
- To resolve data inconsistency problems.
- To prevent thread interference.
Types of Synchronization
There are two types of synchronization in java multithreading.
- Process Based Synchronization.
- Thread Based Synchronization.
Here we will discuss only thread based synchronization.
Thread Synchronization in Java with Example
There are two types of thread synchronization in java and these are...
1) Mutual Exclusive
- Synchronized method
- Synchronized block
- Static Synchronization
2) Inter-Thread Communication
Where we can use synchronization in java program
Synchronization is a modifier in java and we can apply it with method or block only. We cannot apply it with class and variables.
Java synchronized keyword is used to define any method or block as synchronized method or block.
Concept of Lock in Java
Synchronization is built around an internal entity known as the monitor or lock. Each object in java is associated with a monitor which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor.
Let's understand with examples.
Let's understand with examples.
Problem without using synchronization
In this example we are not going to use synchronized keyword. Output will be inconsistent because there is no use of java synchronized keyword.
class Table
{
void displayTable(int n)//without using synchronized keyword
{
for(int i = 1; i <= 5; i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Thread1 extends Thread
{
Table t;
Thread1(Table t)
{
this.t = t;
}
public void run()
{
t.displayTable(5);
}
}
class Thread2 extends Thread
{
Table t;
Thread2(Table t)
{
this.t = t;
}
public void run()
{
t.displayTable(100);
t.displayTable(100);
}
}
class Test
{
public static void main(String args[])
{
Table tt =new Table();
Table tt =new Table();
Thread1 one = new Thread1(tt);
Thread2 two = new Thread2(tt);
one.start();
two.start();
}
}
Output: 5
100
200
10
300
15
20
400
500
25
The above data are displayed with inconsistency.
Output: 5
Above output with consistent data by using synchronized method in java multithreading.
{
Output: 5
Output: 5
100
200
10
300
15
20
400
500
25
The above data are displayed with inconsistency.
Java Synchronized Method
This is simple java synchronized method example where we will use synchronized keyword with method for displaying consistent data on the console.
class Table
{
synchronized void displayTable(int n)//with synchronized keyword
{
for(int i = 1; i <= 5; i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Thread1 extends Thread
{
Table t;
Thread1(Table t)
{
this.t = t;
}
public void run()
{
t.displayTable(5);
}
}
class Thread2 extends Thread
{
Table t;
Thread2(Table t)
{
this.t = t;
}
public void run()
{
t.displayTable(100);
t.displayTable(100);
}
}
class Test
{
public static void main(String args[])
{
Table tt = new Table();
Table tt = new Table();
Thread1 one = new Thread1(tt);
Thread2 two = new Thread2(tt);
one.start();
two.start();
}
}
Output: 5
10
15
20
25
100
200
300
400
500
Above output with consistent data by using synchronized method in java multithreading.
Java Synchronized Block
This is simple java synchronized block. By the help of synchronized block you can perform synchronization on particular resource of method.
Let's understand java synchronized block in multithreading with simple example.
class Table
{
void displayTable(int n)
{
synchronized(this)//synchronized block in java{
for(int i = 1; i <= 5; i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
}
class Thread1 extends Thread
{
Table t;
Thread1(Table t)
{
this.t = t;
}
public void run()
{
t.displayTable(5);
}
}
class Thread2 extends Thread
{
Table t;
Thread2(Table t)
{
this.t = t;
}
public void run()
{
t.displayTable(100);
t.displayTable(100);
}
}
class Test
{
public static void main(String args[])
{
Table tt = new Table();
Table tt = new Table();
Thread1 one = new Thread1(tt);
Thread2 two = new Thread2(tt);
one.start();
two.start();
}
}
Output: 5
10
15
20
25
100
200
300
400
500
Java Static Synchronization
This is simple static synchronization example in java multithreading when you apply synchronized keyword with any static method this known as static synchronization in multithreading.
class Table
{
synchronized static void displayTable(int n)//static synchronization
{
for(int i = 1; i <= 5; i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Thread1 extends Thread
{
public void run()
{
Table.displayTable(5);//calling with class name
}
}
class Thread2 extends Thread
{
public void run()
{
Table.displayTable(100);//calling with class name
Table.displayTable(100);//calling with class name
}
}
class Test
{
public static void main(String args[])
{
Table tt = new Table();
Table tt = new Table();
Thread1 one = new Thread1();
Thread2 two = new Thread2();
one.start();
two.start();
}
}
Here we have learned synchronization in multithreading in java with examples like java synchronized method, synchronized block in java, static synchronization in java.
Here we have learned synchronization in multithreading in java with examples like java synchronized method, synchronized block in java, static synchronization in java.
Thanks for sharing this informative blog java training in chennai
ReplyDeleteI was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore