Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday 17 November 2017

What is Synchronization in Java with Example

What is Synchronization in Java

Java synchronized keyword

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.


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.

  1. Process Based Synchronization.
  2. 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.


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);
}
}

class Test
{
public static void main(String args[])
{
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.


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);
}
}

class Test
{
public static void main(String args[])
{
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);
}
}

class Test
{
public static void main(String args[])
{
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
}
}

class Test
{
public static void main(String args[])
{
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.


Share:

2 comments:

  1. Thanks for sharing this informative blog java training in chennai

    ReplyDelete
  2. I 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.
    Java Training in Bangalore

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate