«Back to Home

Core Java

Topics

Synchronized Block In Java

Synchronized Block
 
In Java, Synchronized block can used to achieve synchronization on any particular resource of the method.
 
Assume that we have 100 lines of code in our method but we want to synchronize it only 10 lines then we can use synchronized block. If we put all the codes of the method in the synchronized block, it will work in similar fashion, as the synchronized method.
 
Important points for Synchronized block
  • This block is mainly used to lock an object for any shared resource.

  • Its scope is smaller than the method.
Syntax

synchronized (object reference expression) {
//code
}
 
Let's see an example of the synchronized block, given below.
 
Code
  1. class B {  
  2.     void print(int n) {  
  3.         synchronized(this) {  
  4.             for (int i = 1; i <= 3; i++) {  
  5.                 System.out.println(n * i);  
  6.                 try {  
  7.                     Thread.sleep(1500);  
  8.                 } catch (Exception e) {  
  9.                     System.out.println(e);  
  10.                 }  
  11.             }  
  12.         }  
  13.     }  
  14. }  
  15. class Thread1 extends Thread {  
  16.     B b;  
  17.     Thread1(B b) {  
  18.         this.b = b;  
  19.     }  
  20.     public void run() {  
  21.         b.print(1);  
  22.     }  
  23. }  
  24. class Thread2 extends Thread {  
  25.     B b;  
  26.     Thread2(B b) {  
  27.         this.b = b;  
  28.     }  
  29.     public void run() {  
  30.         b.print(100);  
  31.     }  
  32. }  
  33. public class SynchronizationBlock {  
  34.     public static void main(String args[]) {  
  35.         B obj = new B();  
  36.         Thread1 t1 = new Thread1(obj);  
  37.         Thread2 t2 = new Thread2(obj);  
  38.         t1.start();  
  39.         t2.start();  
  40.     }  
  41. }  
9
10
Output

11

Let’s see an example of synchronized block with annonymous class.
 
Code
  1. class B {  
  2.     void print(int n) {  
  3.         synchronized(this) {  
  4.             for (int i = 1; i <= 3; i++) {  
  5.                 System.out.println(n * i);  
  6.                 try {  
  7.                     Thread.sleep(1500);  
  8.                 } catch (Exception e) {  
  9.                     System.out.println(e);  
  10.                 }  
  11.             }  
  12.         }  
  13.     }  
  14. }  
  15. public class SynchronizationBlock {  
  16.     public static void main(String args[]) {  
  17.         final B b = new B();  
  18.         Thread t1 = new Thread() {  
  19.             public void run() {  
  20.                 b.print(1);  
  21.             }  
  22.         };  
  23.         Thread t2 = new Thread() {  
  24.             public void run() {  
  25.                 b.print(100);  
  26.             }  
  27.         };  
  28.         t1.start();  
  29.         t2.start();  
  30.     }  
  31. }  
12

Output

13

Summary

Thus, we learnt Java synchronized block can used to achieve synchronization on any particular resource of the method and also learnt how we can create it in Java.