Synchronization

Synchronization
  • synchronized is the keyword applicable for the methods and blocks. We can’t apply this keyword for variables and classes.
  • If a method declared as synchronized at a time only one thread is allowed to execute that method on the given object.
  • The main advantage of synchronized keyword is we can overcome data inconsistency problem.
  • 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.
  • Every object in java has a unique lock. When ever are using synchronized keyword then object level lock concept will come into picture.
  • If a thread want to execute any synchronized method on the object first it should require the lock of that object. Once a thread got the lock then it is allowed to execute any synchronized method on that object.
  • 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.
class Display {
   public synchronized void wish(String name) {
      for (int i = 0; i < 5; i++) {
         System.out.print("Hello ");
         try {
            Thread.sleep(1000);
         } catch (InterruptedException e) {
         }
         System.out.println(name);
      }
   }
}

class MyThread extends Thread {
   Display d;
   String name;

   MyThread(Display d, String name) {
      this.d = d;
      this.name = name;
   }

   public void run() {
      d.wish(name);
   }
}

public class MySynchronized {
   public static void main(String[] args) {
      Display d = new Display();
      MyThread t1 = new MyThread(d, "Ashok");
      MyThread t2 = new MyThread(d, "Vinod");
      t1.start();
      t2.start();
   }
}

Output

Hello Ashok
Hello Ashok
Hello Ashok
Hello Ashok
Hello Ashok
Hello Vinod
Hello Vinod
Hello Vinod
Hello Vinod
Hello Vinod

If we are not declaring wish() method as synchronized then both Threads will be executed simultaneously and we will get irregular output.

Hello Hello Ashok
Hello Vinod
Hello Ashok
Hello Vinod
Hello Ashok
Vinod
Hello Hello Ashok
Hello Vinod
Hello Vinod
Ashok

Case 1

Display d1 = new Display();
Display d2 = new Display();
MyThread t1 = new MyThread(d1, "Ashok");
MyThread t2 = new MyThread(d2, "Vinod");
t1.start();
t2.start();

Even though we declared wish() method as synchronized but we will get irregular output in this case, because both Threads are operating on different objects.

Hello Hello Vinod
Hello Ashok
Hello Ashok
Hello Vinod
Hello Ashok
Hello Vinod
Hello Ashok
Hello Vinod
Hello Ashok
Vinod

Conclusion

If multiple threads are operating on multiple objects then there is no impact of Synchronization. If multiple threads are operating on same java objects then synchronized concept is required(applicable).

Class level lock
  • Every class in java has a unique lock. If a Thread wants to execute a static synchronized method then it required class level lock.
  • Once a Thread got class level lock then it is allow to execute any static synchronized method of that class.
  • While a Thread executing any static synchronized method the remaining Threads are not allow to execute any static synchronized method of that class simultaneously.
  • But remaining Threads are allowed to execute normal synchronized methods, normal static methods, and normal instance methods simultaneously.
  • Class level lock and object lock both are different and there is no relationship between these two.
Synchronized block
  • If very few lines of the code required synchronization then it’s never recommended to declare entire method as synchronized we have to enclose those few lines of the code with in synchronized block.
  • The main advantage of synchronized block over synchronized method is it reduces waiting time of Thread and improves performance of the system.

E.g

To get lock of current object we can declare synchronized block as follows. If Thread got lock of current object then only it is allowed to execute this block.

Synchronized(this){

}

To get the lock of a particular object ‘b’ we have to declare a synchronized block as follows. If thread got lock of ‘b’ object then only it is allowed to execute this block.

Synchronized(b) {

}

To get class level lock we have to declare synchronized block as follows.

Synchronized(Display.class){

}

If thread got class level lock of Display then only it allowed to execute this block.

Note

As the argument to the synchronized block we can pass either object reference or “.class file” and we can’t pass primitive values as argument [because lock concept is dependent only for objects and classes but not for primitives].

Synchronization

Scroll to top