Wednesday, 5 September 2018

Multi-threading Synchronization in Java with Synchronized Block

                        In this post, we will see how to achieve multi threading synchronization in Java using Synchronized block.

                        In previous post, we have seen Multi-threading Synchronization using synchronized method. Check this link: https://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-example-programs.html 

How synchronized block is different from synchronized method? 
                      If we are using synchronized method, then we are going to synchronize all statements in that method. But, if shared resources are used in few statements, then unnecessarily we are are synchronizing remaining statements. This in turn, will result into the wastage of time. To avoid this, synchronized blocks are used.

What is synchronized block?
                     A group (block) of statements where common resources are accessed are declared as synchronized block as follows:

synchronized(this)
{
st1
st2
.
.
.
stn
}

                    Here, 'this' is the reference of current object.

Check following program without using synchronized block:
  
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.

                               Now, let us use synchronized block. Check following program:
   
Program (synchronization.java)

class T1 
{
  int p=0;

  public void add()
   {
     /* Statements not to be synchronized, mention here
     */ 
    
     synchronized(this)
       { 
         p=p+1;
       }
     
    /* Statements not to be synchronized, mention here
    */ 
   }
}

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 program. p=p+1 was the critical section where common resource (variable) p was used. We mentioned that statement in synchronized block inside method add(). Statements of add() method, which we do not want to synchronize, keep them outside the synchronized block.
                       Here, each time, we are getting value of p as 300 as only one thread is allowed in synchronized block at once while all other threads were waiting.

                       How to use 'Synchronized static method', check link https://www.comrevo.com/2019/08/Multi-threading-Synchronization-in-Java-with-Synchronized-Static-Method.html.

No comments:

Post a comment