Having multiple threads in a program may cause inconsistency and thread interference, that is the multiple threads when trying to access the same resource. One thread may try to write and another may try to read the shared resource or same file. So to achieve the use of only one resource or file at a time, we use synchronization.
Synchronization makes sure that only one out of the multiple threads is allowed to access the resource at a given time.
In this process, comes the Lock Concept, also known as monitors.
Each object created in java is provided with the monitor. Only one thread may hold the lock at a time through which thread can lock or unlock.
Synchronized method in java:
Java contains a synchronized method to apply synchronous behaviour to the program. This method is used to lock an object for shared resources. The object automatically obtained a lock when it invokes the synchronized method and releases when the work is done.
Syntax for Synchronized method:
1 2 3 4 5 | synchronized(object) { //synchronized statement // Access shared variables and other shared resources } |
Example of Java program without the use synchronized method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | class Numbers { void displaySum(int a)//method not synchronized { for(int i=1;i<=4;i++) { System.out.println(a+i); try { Thread.sleep(200); }catch(Exception e) { System.out.println(e); } } } } class Thread1 extends Thread { Numbers n; Thread1(Numbers n) { this.n = n; } public void run() { n.displaySum(1); } } class Thread2 extends Thread { Numbers n; Thread2(Numbers n) { this.n = n; } public void run() { n.displaySum(20); } } public class Main { public static void main(String args[]) { Numbers obj = new Numbers(); Thread1 th1=new Thread1(obj); Thread2 th2=new Thread2(obj); th1.start(); th2.start(); } } |
Output:
1 2 3 4 5 6 7 8 | 2 21 22 3 23 4 24 5 |
Now let see an example with the use synchronized method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | class Numbers { synchronized void displaySum(int a)//method synchronized { for(int i=1;i<=4;i++) { System.out.println(a+i); try { Thread.sleep(200); }catch(Exception e) { System.out.println(e); } } } } class Thread1 extends Thread { Numbers n; Thread1(Numbers n) { this.n = n; } public void run() { n.displaySum(1); } } class Thread2 extends Thread { Numbers n; Thread2(Numbers n) { this.n = n; } public void run() { n.displaySum(20); } } public class Main { public static void main(String args[]) { Numbers obj = new Numbers(); Thread1 th1=new Thread1(obj); Thread2 th2=new Thread2(obj); th1.start(); th2.start(); } } |
Output:
1 2 3 4 5 6 7 8 | 2 3 4 5 21 22 23 2 |
Synchronized block in java:
It is also used to achieve synchronization in java which locks an object for any shared resource. It is the same as a synchronized method.
Using Synchronized block only selected lines of code can be synchronized.
Syntax for Synchronized block:
1 2 3 4 | synchronized (object reference) { //code block need to synchronized } |
Example to demonstrate the use of Synchronized block in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | class Numbers { void displaySum(int a) { synchronized(this)// synchronized block { for(int i=1;i<=4;i++) { System.out.println(a+i); try { Thread.sleep(200); }catch(Exception e) { System.out.println(e); } } } } } class Thread1 extends Thread { Numbers n; Thread1(Numbers n) { this.n = n; } public void run() { n.displaySum(1); } } class Thread2 extends Thread { Numbers n; Thread2(Numbers n) { this.n = n; } public void run() { n.displaySum(20); } } public class Main { public static void main(String args[]) { Numbers obj = new Numbers(); Thread1 th1=new Thread1(obj); Thread2 th2=new Thread2(obj); th1.start(); th2.start(); } } |
Output:
1 2 3 4 5 6 7 8 | 2 3 4 5 21 22 23 24 |
Note that:
The scope of the synchronized method is higher than the synchronized block.