1. ‘synchronized’ is the keyword applicable for the methods and blocks.
  2. We can’t apply this keyword for variables and classes.
  3. If a method declared as synchronized, at a time,only one thread is allowed to execute that method on the given object.
  4. The main advantage of synchronized keyword is, we can overcome data inconsistency problem.
  5. The main limitation of synchronized keyword is it may create preference problems.Hence if there is no specific requirement it’s not recommended to use synchronized keyword.
  6. Every object in java has a unique lock.When ever we are using synchronized keyword then object level lock concept will come into picture.
  7. If a thread want to execute any synchronized method on the object first it should get the lock of that object.
  8. Once a thread got the lock then it is allowed to execute any synchronized method on that object.
  9. While a thread executing a synchronized method on the object, then the remaining threads are not allowed to execute any synchronized method on the same object. But the remaining threads are allowed to execute any non-synchronized method on the same object.
  10. Every object in java has unique lock but a thread can acquire more than one lock at a time.

Example:

class Display {
	 
    public synchronized void  display(String name) {
        for (int i = 1; i <= 3; i++) {
            try {
                Thread.sleep(2000);
            } catch (Exception exception) {
 
            }
            System.out.println(name);
        }
    }
 
}
 
class SynchronizedThread extends Thread {
 
    Display display;
    String name;
 
    SynchronizedThread(Display display, String name) {
        this.display = display;
        this.name = name;
    }
 
    public void run() {
        display.display(name);
    }
}
 
public class SynchronizedDemo {
    public static void main(String[] args) {
        Display d1 = new Display();
        SynchronizedThread thread1 = new SynchronizedThread(d1,
                "Hello");
        SynchronizedThread thread2 = new SynchronizedThread(d1,
                "Hai");
        thread1.start();
        thread2.start();
    }
}
/*
Output:
Hello
Hello
Hello
Hai
Hai
Hai
*/
  1. If we are not declaring display method as “synchronized” we will get irregular output because both threads will execute simultaneously.
  2. If we are declaring display method as synchronized we will get regular output because at a time only one thread is allowed to execute wish method.

Note:

Display d1 = new Display();
Display d2 = new Display();
SynchronizedThread t1 = new SynchronizedThread(d1,"Hello");
SynchronizedThread t2 = new SynchronizedThread(d2,"Hai");
t1.start();
t2.start();

Even though display method is synchronized we will get irregular output because both threads are operating on different objects.Synchronization concept is applicable only on a single common object.

Class level lock

  1. Every class in Java has a unique lock.
  2. If a thread wants to execute any static synchronized method then compulsory that thread should require class level lock.
  3. While a thread executing any static method then the remaining threads are not allowed to execute any static synchronized method of the same class simultaneously.But the remaining threads are allowed to execute any non-synchronized static methods, synchronized instance methods, non synchronized instance method simultaneously.
  4. Declare static synchronized in display method and try the above example we will get regular output because there is class level lock.

Synchronized Blocks

  1. It is not recommended to declare entire method as synchronized if very few lines of code causes the problem .That few lines of code we can declare inside synchronized block, so that we can improve performance of the system.

    Syntax:

    synchronized(b){
    //critical section.
    }
    

    Where ‘b’ is an object reference.

  2. To get the lock for the current object we can define synchronized block as follows
    synchronized(this){
    //critical section.
    }
    
  3. We can define synchronized block to get class level as follows
    synchronized(Display.class)
    
  4. we can define synchronized block either for object references or for class references But not for primitives violation leads to Compile time error.
    int i =10;
    synchronized(i){
    //code
    }
    /*
    Compile time error: unexpected type
    found : int.
    required : reference.
    */
    
  5. The statements which are defined in inside synchronized method or synchronized blocks are called ‘synchronized statements
  6. .