Java`s synchronized, A way to prevent data from being corrupted by multiple threads is to prevent the interruption of critical regions.Java`s synchronized can be used to ensure that only one thread is in a critical region at once While the thread is in the critical region, no other thread can enter the critical region. If a thread tries to enter and the code is already locked, the thread has to wait for the other thread to leave the critical region. Sometimes, multiple threads may be accessing the same resources concurrently
  -Reading and / or writing the same file
  -Modifying the same object / variable Synchronization controls thread execution order Synchronization eliminates data races .Java has built in primitives to facilitate this coordination

Synchronization can be applied on a method or there can be a synchronized block.

Public void synchronized method1()
{
Task to do……
}

Or

Public void run()
{
 Synchronized(this) or synchronized(object){
   Task to do…..
 }
}

Both the ways are there to synchronize but using synchronize block we can syncronize an object means we can write something like synchronized(Object.class){}.Whereas synchronized method will synchronize only calling object that is “this”.

Here below the demo for Synchronized method

class sdisp
{
    synchronized public void disp(long ti)
    {
    System.out.print("<");
    for(int i=1;i<=5;i++)
        {
            System.out.print(i+" ");
            try
                {
                Thread.sleep(ti);
               }catch(InterruptedException e)
            {
            System.out.println(e);
            }
        }
    System.out.println(">");
}
}
class sth implements Runnable
{
    sdisp s=new sdisp();
       
    Thread t=new Thread(this);
    long ti;
    public sth(long h,sdisp s)
    {
    ti=h;
    t.start();
    this.s=s;
    }
   
    public void run()
    {

    s.disp(ti);
   }
}
class msth

{
    public static void main(String[] args)
    {
        System.out.println("Main started..");
        sdisp ss=new sdisp();
        sth s=new sth(100,ss);
        sth s1=new sth(100,ss);
            try
                {
                Thread.sleep(1000);
               }catch(InterruptedException e)
                     {
                     System.out.println(e);
                    }

    }
}


Here below the demo for Synchronized Block

class sdisp
{
    public void disp(long ti)
    {
    System.out.print("<");
    for(int i=1;i<=5;i++)
        {
            System.out.print(i+" ");
            try
                {
                Thread.sleep(ti);
               }catch(InterruptedException e)
            {
            System.out.println(e);
            }
        }
    System.out.println(">");
}
}
class sth implements Runnable
{
    sdisp s=new sdisp();
       
    Thread t=new Thread(this);
    long ti;
    public sth(long h,sdisp s)
    {
    ti=h;
    t.start();
    this.s=s;
    }
   
    public void run()
    {
    synchronized(s)
        {
        s.disp(ti);
        }
    }
}
class msbth

{
    public static void main(String[] args)
    {
        System.out.println("Main started..");
        sdisp ss=new sdisp();
        sth s=new sth(100,ss);
        sth s1=new sth(100,ss);
            try
                {
                Thread.sleep(1000);
               }catch(InterruptedException e)
                     {
                     System.out.println(e);
                    }

    }
}