Synchronization in Java

  • By
  • July 5, 2021
  • Uncategorized

Synchronization is concept belongs to threading in java, with this concept we able to control the access of multiple thread objects to shared resource. it is better choice where we wants to allow only one thread object to access the shared resource memory.

The main reason behind using synchronization is, 

To prevent the thread objects interference and 

To prevent consistency problem in threading

=> Generally synchronization we can do in two ways,

**  Without Synchronization: without synchronization all thread object will execute in concurrent way.

package com.without.synchronizaation;

public class Example1 extends Thread

{

Table t1;

public Example1(Table t1) 

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

{

this.t1 = t1;

}

public void run()

{

t1.printTable(20);

}

}

—————————————————————————————————-

package com.without.synchronizaation;

public class Example2 extends Thread

{

Table t1;

public Example2(Table t1) 

{

this.t1 = t1;

}

public void run()

{

t1.printTable(50);

}

}

—————————————————————————————————-

package com.without.synchronizaation;

public class Table 

{

public void printTable(int number)

{

for(int i = 1; i <= 5; i++)

{

System.out.println(i*number);

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

System.out.println(e);

}

}

}

 

}

—————————————————————————————————-

package com.without.synchronizaation;

public class TestClass 

{

public static void main(String[] args) 

{

Table table1 = new Table();

Example1 example1 = new Example1(table1);

Example2 example2 = new Example2(table1);

example1.start();

example2.start();

}

}

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

From the above java program Example1 and Example2 are thread objects, they are sharing resource in parallel way. 

Internally using thread scheduling concept to execute thread objects. Either they can use preemptive approach or time slicing approach.

—————————————————————————————————

With Synchronization: by using,

  1. Process-based Synchronization: Here we not covering this process-based synchronization concept.
  2. Thread-based Synchronization:

But here we cover thread-based synchronization, again thread-based synchronization is divided into two types,

Mutual Exclusive Thread-based synchronization: this type of approach we will implement by using,

  1. Synchronized method concept: In this type of synchronization approach we are applying lock on method, so the method allows to use only thread object.

Until object release other thread objects need to wait for resource.

package com.with.synchronization.method;

public class Example1 extends Thread

{

Table t1;

public Example1(Table t1) 

{

this.t1 = t1;

}

public void run()

{

t1.printTable(20);

}

}

—————————————————————————————————-

package com.with.synchronization.method;

public class Example2 extends Thread

{

Table t1;

public Example2(Table t1) 

{

this.t1 = t1;

}

public void run()

{

t1.printTable(50);

}

}

—————————————————————————————————-

package com.with.synchronization.method;

 

public class Table 

{

//non-static method

public synchronized void printTable(int number)

{

for(int i = 1; i <= 5; i++)

{

System.out.println(i*number);

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

System.out.println(e);

}

}

}

 

}

—————————————————————————————————-

package com.with.synchronization.method;

public class TestClass 

{

public static void main(String[] args) 

{

Table table1 = new Table();

Example1 example1 = new Example1(table1);

Example2 example2 = new Example2(table1);

example1.start();

example2.start();

}

}

==========================================================


  1. Synchronized block concept:

The Synchronized block used to perform the synchronization on specific resource/shared resource of the method java program.

Let us consider there is java program, we have 100 lines of code in method, and we want to apply synchronization only on 10 lines, so synchronized block is better option.

Suppose If we take all the code of the method in synchronized block, it will behave same as the synchronized java method.

 

package com.with.synchronization.block;

public class Example1 extends Thread

{

Table t1;

public Example1(Table t1) 

{

this.t1 = t1;

}

public void run()

{

t1.printTable(200);

}

}

—————————————————————————————————-

package com.with.synchronization.block;

public class Example2 extends Thread

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

{

Table t1;

public Example2(Table t1) 

{

this.t1 = t1;

}

public void run()

{

t1.printTable(500);

}

}

—————————————————————————————————-

package com.with.synchronization.block;

public class Table 

{

public void printTable(int number)

{

synchronized (this) 

{

for(int i = 1; i <= 5; i++)

{

System.out.println(i*number);

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

System.out.println(e);

}

}

}

}

}

—————————————————————————————————-

package com.with.synchronization.block;

public class TestClass 

{

public static void main(String[] args) 

{

Table table1 = new Table();

Example1 example1 = new Example1(table1);

Example2 example2 = new Example2(table1);

example1.start();

example2.start();

}

}

==========================================================

  1. Static synchronization concept:

If we want to make any java static method as synchronized, then we apply lock on the class but not on thread object.

Consider there are two thread objects of a shared class named thread1 and thread2. 

So in case of synchronized java method and synchronized java block, there is no  interference between thread1 and thread2 or thread3 and thread4 because thread1 and thread2 both shares a common thread object that have single lock. 

But there is interference between thread1 and thread3 or thread2 and thread4, because thread1 acquires another lock and thread3 acquires another lock. 

So if we want no interference between thread1 and thread3 or thread2 and thread4. So Static synchronization will solves this issue, we declare synchronized with static keyword.

package com.with.synchronization.StaticMethod;

public class Example1 extends Thread

{

public void run()

{

Table.printTable(10);

}

}

—————————————————————————————————-

package com.with.synchronization.StaticMethod;

 

public class Example2 extends Thread

{

public void run()

{

Table.printTable(50);

}

}

—————————————————————————————————-

package com.with.synchronization.StaticMethod;

public class Example3 extends Thread

{

public void run()

{

Table.printTable(30);

}

}

—————————————————————————————————-

package com.with.synchronization.StaticMethod;

public class Example4 extends Thread

{

public void run()

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

{

Table.printTable(300);

}

}

—————————————————————————————————-

package com.with.synchronization.StaticMethod;

public class Table 

{

//static method

public static synchronized void printTable(int number)

{

for(int i = 1; i <= 5; i++)

{

System.out.println(i*number);

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

System.out.println(e);

}

}

}

}

—————————————————————————————————-

package com.with.synchronization.StaticMethod;

public class TestClass 

{

public static void main(String[] args) 

{

Example1 example1 = new Example1();

Example2 example2 = new Example2();

Example3 example3 = new Example3();

Example4 example4 = new Example4();

example1.start();

example4.start();

example2.start();

example3.start();

}

}

==========================================================

Inter-thread communication in Java:

The Cooperation or Inter-thread communication is a technique, in which a thread object is paused running in its critical section, and another thread object is allowed to enter or lock in the same critical section executed.

package com.inter.thred.communication;

 

public class Example1 

{

public static void main(String[] args) 

{

final String resource1 = “Ratan”; 

 

final String resource2 = “Vimal”;

 

Thread t1 = new Thread()

{

public void run()

{

synchronized (resource1) 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

{

System.out.println(“Thread-1: locked resource1”);    

        try 

       

        Thread.sleep(100);

       

        catch (Exception e) {} 

        synchronized(resource2) 

        {  

            System.out.println(“Thread-1: locked resource2”);  

       

}

}

};

Thread t2 = new Thread()

{

public void run()

{

synchronized (resource1) 

{

System.out.println(“Thread-2: locked resource2”);  

try 

       

        Thread.sleep(100);

       

        catch (Exception e) {}         

        synchronized(resource2) 

        {  

            System.out.println(“Thread-2: locked resource1”);  

       

}

}

};

t1.start();

t2.start();

}

}

 

Conclusion: The synchronization is best of technology to prevent thread interference and consistency problem in java threads.

Author:- Patil Namdev


Call the Trainer and Book your free demo Class for JAVA now!!!

© Copyright 2021 | Sevenmentor Pvt Ltd.

 

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*