Synchronized Block in Java

Introduction

 
In this article, we are going to discuss synchronization in Java. Now we are only discussing where and how we use a Synchronized block in Java. You can also achieve synchronization using a synchronized method. It's very easy; just use the synchronized keyword in front of the method and then your method is synchronized.
 

What is Synchronization?

 
synchronized-server.gif 
 
Synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to an error.
 

Synchronized block in Java

 
In Java, a synchronized block is another way to execute code synchronized. The block is provided an object to allow execution of a block of code to be synchronized on the lock of this object.
 
Syntax
 
synchronized(this)
{
//write here your code
}
 

Why use Synchronization?

 
When multiple threads access the same data item (resource) and more than one thread wants to change the resources then it can happen that two threads change the resource simultaneously which can corrupt the data. Suppose one thread increments a data item and another thread decrements the same data item; this may cause data corruption. So that is why we use synchronization.
 
Note:
Every Java object created, including every Class loaded, has an associated lock or monitor. Putting code inside a synchronized block makes the compiler append instructions to acquire the lock on the specified object before executing the code, and release it afterwards (either because the code finishes normally or abnormally). Between acquiring the lock and releasing it, a thread is said to "own" the lock. At the point the thread wants to acquire the lock, if another thread already owns it, then the thread attempting to get a lock must wait for the other thread to release it.
 
Example
  1. package ab;  
  2. class Callme  
  3. {  
  4.     void call(String msg)  
  5.     {  
  6.         System.out.print("[" + msg);  
  7.         try  
  8.         {  
  9.             Thread.sleep(2000);  
  10.         } catch (InterruptedException e)  
  11.         {  
  12.             System.out.println("Interrupted");  
  13.         }  
  14.         System.out.println("]");  
  15.     }  
  16. }  
  17. class Caller implements Runnable  
  18. {  
  19.     String msg;  
  20.     Callme target;  
  21.     Thread t;  
  22.     public Caller(Callme targ, String s)  
  23.     {  
  24.         target = targ;  
  25.         msg = s;  
  26.         t = new Thread(this);  
  27.         t.start();  
  28.     }  
  29.     public void run()  
  30.     {  
  31.         synchronized(target)  
  32.         {  
  33.             // synchronized block  
  34.             target.call(msg);  
  35.         }  
  36.     }  
  37. }  
  38. class Synch1  
  39. {  
  40.     public static void main(String args[])  
  41.     {  
  42.         Callme target = new Callme();  
  43.         Caller ob1 = new Caller(target, "abhishek");  
  44.         Caller ob2 = new Caller(target, "Synchronized");  
  45.         Caller ob3 = new Caller(target, "Dubey");  
  46.         Caller ob4 = new Caller(target, "MCA");  
  47.         Caller ob5 = new Caller(target, "2012 batch");  
  48.         try  
  49.         {  
  50.             ob1.t.join();  
  51.             ob2.t.join();  
  52.             ob3.t.join();  
  53.             ob4.t.join();  
  54.             ob5.t.join();  
  55.         } catch (InterruptedException e)  
  56.         {  
  57.             System.out.println("Interrupted");  
  58.         }  
  59.     }  
  60. }  
Output
 
cmd image.jpg