- If very few lines of code requires synchronization then it is not recommended to declare entire method as synchronized, we have to enclose those few lines of code by using synchronized block.
- The main advantage of synchronized block over synchronized method is, it reduces waiting time of threads and improves performance of the application.
We can declare synchronized block as follows :
1. To get lock of current object
synchronized(this){ //line of codes //If a thread got lock of current object then only it is allowed to execute this area }
2. To get lock of particular object
synchronized(object){ //line of codes //If a thread got lock of particular object b then only it is allowed to execute this area }
3. To get class level lock
synchronized(Display.class){ //line of codes //If a thread got class level lock of display class then only it is allowed to execute this area }
Example1 : (lock of current object)
package com.demo2; import java.util.stream.IntStream; public class ThreadDemo { public static void main(String[] args) { Greet greet = new Greet(); new GreetThread(greet,"Tyson").start(); new GreetThread(greet,"Justin").start(); new GreetThread(greet,"Martin").start(); } } class Greet{ public void display(String name) { synchronized(this) { IntStream.range(1,5).forEach(x -> System.out.println(name+" : "+x)); } } } class GreetThread extends Thread{ String name; Greet greet; GreetThread(Greet greet,String name){ this.greet = greet; this.name = name; } public void run() { greet.display(name); } }
Example 2 : Lock of current class
package com.demo2; import java.util.stream.IntStream; public class ThreadDemo { public static void main(String[] args) { Greet greet = new Greet(); new GreetThread(greet,"Tyson").start(); new GreetThread(greet,"Justin").start(); new GreetThread(greet,"Martin").start(); } } class Greet{ public void display(String name) { synchronized(Greet.class) { IntStream.range(1,5).forEach(x -> System.out.println(name+" : "+x)); } } } class GreetThread extends Thread{ String name; Greet greet; GreetThread(Greet greet,String name){ this.greet = greet; this.name = name; } public void run() { greet.display(name); } }
Note : Lock concept applicable for object types and class types but not for primitives hence we can’t pass primitive type as argument to synchronized block, other wise we will get compile time error saying unexpected type found int required reference.
Important Note :It’s possible that both static synchronized and non static synchronized method can run simultaneously or concurrently because they lock on different object.
FAQ :
- What is synchronized keyword where we can apply.
- Explain advantages of synchronized keyword.
- Explain disadvantages of synchronized keyword.
- What is race condition.
If multiple threads access the same java object then it might cause data inconsistency problem, this is called as race condition.We can resolve it by using synchronized keyword. - What is a object lock and when it is required?
- What is a class level lock and when it is required?
- What is the difference between class level and object level lock?
While a thread executing synchronized method on the given object is the remaining thread allowed to execute any other synchronized method simultaneously on the same object