Tuesday, 6 August 2019

Multi-threading Synchronization in Java with Synchronized Static Method

                        In this post, we will see Multi-threading Synchronization in Java with Synchronized Static Method.

                        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 .

                       Also, we have seen how to use static block in this link: https://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-synchronized-block.html .

                       In this post, we will use synchronization for static method.

How static method is different from non-static method? 
                      In order to declare any method static, we have to use keyword static in method definition. In static method, we can only use static variables. Static methods can be called by class name directly. No need to create object.


                      Check following program without synchronization:
  
Program (SynchronizationStatic.java)
class T1
{
  static int p=0;

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

class T extends Thread
{
  public void run()
  {
    T1.add();   
  }
}

class SynchronizationStatic
{
  public static void main(String args[]) throws Exception
  {
  int i;
 
  T q[]=new T[300];
 
  for(i=0;i<300;i++)
     q[i]=new T();

  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/programs/thread$ javac SynchronizationStatic.java
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:294
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:299
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:297
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:299
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:299

  
                              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 keyword. Check following program:
   
Program (SynchronizationStatic.java)

class T1
{
  static int p=0;

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

class T extends Thread
{
  public void run()
  {
    T1.add();  
  }
}

class SynchronizationStatic
{
  public static void main(String args[]) throws Exception
  {
  int i;
 
  T q[]=new T[300];
 
  for(i=0;i<300;i++)
     q[i]=new T();

  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/programs/thread$ javac SynchronizationStatic.java 
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
Value of p:300
parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic
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 static method add().
                       Here, each time, we are getting value of p as 300 as only one thread is allowed in synchronized static method at once while all other threads were waiting.

                       How to use 'Multi-threading synchronization using lock', check in next post.

No comments:

Post a comment