Demonstrate Synchronization block - Java

Q. Write a program to demonstrate the Synchronization block.

Answer:

Synchronization:
Synchronization is the process of allowing threads to execute one after another. It provides the control to access multiple threads to shared resources.  When we want to allow one thread to access the shared resource, then synchronization is the better option.

In this example, the synchronized block prevents to create the random thread. The threads are executed sequentially.
  

SynchTest.java

class SynchTest
{  
      void printNumber(int n)
      {  
            synchronized(this)
            {
                  System.out.println("Table of "+n);
                  System.out.println("==========");
                  for(int i=1;i<=10;i++)
                  {  
                        System.out.println(n+" * "+i+" = "+(n*i));  
                        try
                        {  
                              Thread.sleep(400);  
                        }
                        catch(Exception e)
                        {
                              System.out.println(e);
                        }  
                  }  
            }  
      }
}


MyThread1.java

class MyThread1 extends Thread
{  
      SynchTest t;  
      MyThread1(SynchTest t)
      {
            this.t=t;  
      }  
      public void run()
      {
            t.printNumber(7);  
      }  
}


SynchronizedBlock.java

public class SynchronizedBlock
{  
      public static void main(String args[])
      {  
            SynchTest obj = new SynchTest();  
            MyThread1 t1=new MyThread1(obj);
            t1.start();     
      }  
}


Output:

synchronization block