Introduction To Synchronization In Java

Introduction

 
In this article, we discuss Synchronization in Java.
 

Synchronization

 
Synchronization is the capability of controlling access of multi-threads to any shared resources. It is better for when we want only one thread to access the shared resource at a time.
 
Purpose of using Synchronization in Java
 
Synchronization is mainly used to provide safety for:
  1. thread interference.
  2. consistency problem.

Types of Synchronization

 
The following are the two types of synchronization:
  1. thread synchronization
  2. process synchronization
In this article, we discuss only thread synchronization.
 

Thread Synchronization

 
There are of two types of thread synchronization, inner-thread (cooperation) and mutually exclusive communication.
 
Mutually exclusive
 
Mutually exclusive synchronization helps threads to communicate while sharing common data. That can happen in Java by one of the three ways, with the use of a synchronized block, static synchronization and a synchronized method.
 

What is lock?

 
Synchronization is built around an internal entity known as the lock or monitor. Every object has a lock associated with it. By convention, a thread that needs consistent access to an object's fields needs to acquire the object's lock before accessing them, and then release the lock when it's done with them.
 
Without the use of synchronization take a problem
 
In this example, we don't use any synchronization, so the output is inconsistent. Let's have a look.
  1. class Chair      
  2. {      
  3.     void printChair(int i)      
  4.     {      
  5.         for (int x = 1; x <= 6; x++)      
  6.         {      
  7.             System.out.println(i * x);      
  8.             try     
  9.             {      
  10.                 Thread.sleep(350);     
  11.             }     
  12.             catch(Exception ey)    
  13.             {    
  14.                 System.out.println(ey);    
  15.             }    
  16.         }    
  17.     }    
  18. }    
  19. class Thread1 extends Thread    
  20. {    
  21.     Chair ch;    
  22.     Thread1(Chair ch)    
  23.     {    
  24.         this.ch = ch;    
  25.     }    
  26.     public void run()    
  27.     {    
  28.         ch.printChair(6);    
  29.     }    
  30. }    
  31. class Thread2 extends Thread    
  32. {    
  33.     Chair ch;    
  34.     Thread2(Chair ch)    
  35.     {    
  36.         this.ch = ch;    
  37.     }    
  38.     public void run()    
  39.     {    
  40.         ch.printChair(250);    
  41.     }    
  42. }    
  43. class Print    
  44. {    
  45.     public static void main(String args[])    
  46.     {    
  47.         Chair obj = new Chair();    
  48.         Thread1 th1 = new Thread1(obj);    
  49.         Thread2 th2 = new Thread2(obj);    
  50.         th1.start();    
  51.         th2.start();    
  52.     }    
  53. }      
Output
Fig-1.jpg
 
Solution of the previous problem using Synchronization
  • Define the method as synchronized.
  • This synchronized method is used to lock an object for any shared resource.
  • When our thread extends a synchronized method, it contains that lock for the object and releases the lock when the method returns.
  1. class Chair  
  2. {  
  3.     synchronized void printChair(int i)  
  4.     {  
  5.         for (int x = 1; x <= 6; x++)  
  6.         {  
  7.             System.out.println(i * x);  
  8.             try  
  9.             {  
  10.                 Thread.sleep(350);  
  11.             }   
  12.             catch (Exception ey)  
  13.             {  
  14.                 System.out.println(ey);  
  15.             }  
  16.         }  
  17.     }  
  18. }  
  19. class Thread1 extends Thread  
  20. {  
  21.     Chair ch;  
  22.     Thread1(Chair ch)  
  23.     {  
  24.         this.ch = ch;  
  25.     }  
  26.     public void run()  
  27.     {  
  28.         ch.printChair(6);  
  29.     }  
  30. }  
  31. class Thread2 extends Thread  
  32. {  
  33.     Chair ch;  
  34.     Thread2(Chair ch)  
  35.     {  
  36.         this.ch = ch;  
  37.     }  
  38.     public void run()  
  39.     {  
  40.         ch.printChair(250);  
  41.    }  
  42. }  
  43. class Print  
  44. {  
  45.     public static void main(String args[])  
  46.     {  
  47.         Chair obj = new Chair();  
  48.         Thread1 t1 = new Thread1(obj);  
  49.         Thread2 t2 = new Thread2(obj);  
  50.         t1.start();  
  51.         t2.start();  
  52.   
  53.     }  
  54. }   
Output
Fig-2.jpg