Wednesday 5 September 2018

Multi-threading Synchronization in Java with Example Programs

                     In this post, we will see Multi-threading Synchronization in Java with Example Programs.


                     Synchronization in Java can be achieved by using keyword 'synchronized'. It can be used for a method, for a block or for a static method. 
                     So, Synchronization in Java is achieved by 

  • Synchronized Method
  • Synchronized Block
  • Synchronized Static Method 
                     First we will check what is the necessity of Synchronization. 
                      When, we are creating multiple threads; these multiple threads may access common resources, for example, variables. This is called a Race Condition. A part of a program where these common resources are accessed is called a Critical Section. As a result, there may be inconsistency.
                      Check following program, 

Program (Synchronization.java)
class T1 
{
  int p=0;

  public void add()
   {
       p=p+1;
   }
}

class T extends Thread
{
  T1 t1;

  public T(T1 t2)
  {
    this.t1=t2;
  }

  public void run() 
  { 
    t1.add();    
  }
}

class Synchronization
{
  public static void main(String args[]) throws Exception 
  {
  int i;
  T1 t1=new T1();

  T q[]=new T[300];
  
  for(i=0;i<300;i++)
     q[i]=new T(t1);

  for(i=0;i<300;i++)
     q[i].start();
  
  for(i=0;i<300;i++)
     q[i].join();  

  System.out.println("Value of p:"+t1.p);
  }
}


  

Output: 

parag@parag-Inspiron-N4010:~/Desktop/progbythread$ javac Synchronization.java 
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:298
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:299
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:299
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization

Value of p:297

  
                              Check above program and its output. In this program, we are creating 300 threads. All these 300 threads are trying to increment value of variable p which was initialized to 0 in the beginning.  Now check the output, sometimes we are getting value of p as 298, sometimes 300, sometimes 299, and sometimes 297. The reason behind this is few threads are incrementing the value of p simultaneously. 

                               Here, p=p+1 is a critical section. We need to synchronize threads.

What do you mean by synchronizing threads?
                                Synchronizing threads means allowing only one thread to be in a critical section. When one thread is executing statements from critical section, all other threads should wait.

How to achieve Synchronization among multiple threads?
                                In Java, we can achieve synchronization by using keyword 'synchronized'. While defining a method, mention keyword synchronized.
                                 Above program with 'synchronized' keyword is as follows:
Program (synchronization.java)

class T1 
{
  int p=0;

  synchronized public void add()
   {
       p=p+1;
   }
}

class T extends Thread
{
  T1 t1;

  public T(T1 t2)
  {
    this.t1=t2;
  }

  public void run() 
  { 
    t1.add();    
  }
}

class synchronization
{
  public static void main(String args[]) throws Exception 
  {
  int i;
  T1 t1=new T1();

  T q[]=new T[300];
  
  for(i=0;i<300;i++)
     q[i]=new T(t1);

  for(i=0;i<300;i++)
     q[i].start();
  
  for(i=0;i<300;i++)
     q[i].join();  

  System.out.println("Value of p:"+t1.p);
  }
}


  

Output: 
  
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ javac Synchronization.java 
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/progbythread$ java Synchronization

Value of p:300 
    
                         Check above output. Each time, we are getting value of p as 300. Here, we have synchronized threads. So that, only one thread was incrementing the value of p at one time instance.
                          Hope, you have understood the necessity of synchronization and how to achieve it in Java. Here, we have synchronized complete method. But, if you want to synchronize only few statements in a method, then you have to use synchronized block

                           How to use synchronized block, check in Next Post https://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-synchronized-block.html.




No comments:

Post a Comment