Java Thread Synchronization

Java Thread Synchronization :

  1. In multi-threading environment, When two or more threads need access to a any shared resource then their should be some mechanism to ensure that the resource will be used by only one thread at a time.
  2. Thread Synchronization is a process by which this synchronization is achieved.
  3. Thread Synchronization is used to prevent thread interference and consistency problem.
  4. Thread Synchronization is achieved through keyword synchronized.

Types of Synchronization :

Synchronization types
As discussed in the above tree chart, There are two types of thread synchronization :

  1. Mutual Exclusive
    • Synchronized method.
    • Synchronized block.
    • static synchronization.
  2. Cooperation (Inter-thread communication)

Consider the following example without synchronization -

class TableClass {
   void displayTable(int n) {
      for (int i = 1; i <= 5; i++) {
         System.out.println(n * i);
         try {
            Thread.sleep(400);
         } catch (Exception e) {
            System.out.println(e);
         }
      }
   }
}
class MyThread1 extends Thread {
   TableClass c1;
   MyThread1(TableClass tObj) {
      this.c1 = tObj;
   }
   public void run() {
      c1.displayTable(5);
   }
}
class MyThread2 extends Thread {
   TableClass c2;
   MyThread2(TableClass tObj) {
      this.c2 = tObj;
   }
   public void run() {
      c2.displayTable(100);
   }
}
class SynchronizeExample {
   public static void main(String args[]) {
        TableClass obj = new TableClass();
        MyThread1 t1 = new MyThread1(obj);
        MyThread2 t2 = new MyThread2(obj);
        t1.start();
        t2.start();
   }
}

Output :

100
5
10
200
15
300
400
20
25
500 

Now consider the following example using the keyword synchronized -

class TableClass {
   synchronized void displayTable(int n) {
      for (int i = 1; i <= 5; i++) {
         System.out.println(n * i);
         try {
            Thread.sleep(400);
         } catch (Exception e) {
            System.out.println(e);
         }
      }
   }
}
class MyThread1 extends Thread {
   TableClass c1;
   MyThread1(TableClass tObj) {
      this.c1 = tObj;
   }
   public void run() {
      c1.displayTable(5);
   }
}
class MyThread2 extends Thread {
   TableClass c2;
   MyThread2(TableClass tObj) {
      this.c2 = tObj;
   }
   public void run() {
      c2.displayTable(100);
   }
}
class SynchronizeExample {
   public static void main(String args[]) {
        TableClass obj = new TableClass();
        MyThread1 t1 = new MyThread1(obj);
        MyThread2 t2 = new MyThread2(obj);
        t1.start();
        t2.start();
   }
}

Output :

100
200
300
400
500
5
10
15
20
25

Explanation :

When we write the method using synchronized keyword then that method is called as synchronized method.

synchronized void displayTable(int n) {
    for (int i = 1; i <= 5; i++) {
      System.out.println(n * i);
      try {
         Thread.sleep(400);
      } catch (Exception e) {
         System.out.println(e);
      }
   }
}

Above method is synchronized method.

synchronized void displayTable(int n)

Synchronized method is used to lock an object for shared resource. So that when thread invokes a synchronized method then acquires the lock for that object and releases it when the method returns.