Synchronization : Synchronization is a modifier which is used for method and block only. With the help of synchronized modifier we can restrict a shared resource to be accessed only by one thread. When two or more threads need access to shared resources, there is some loss of data i.e. data inconsistency. The process by which we can achieve data consistency between multiple threads it is called Synchronization.
Why do you need Synchronization?
Let us assume if you have two threads that are reading and writing to the same ‘resource’. Suppose there is a variable named as geek, and you want that at one time only one thread should access the variable(atomic way). But Without the synchronized keyword, your thread 1 may not see the changes thread 2 made to geek, or worse, it may only be half changed that cause the data inconsistency problem. This would not be what you logically expect. The tool needed to prevent these errors is synchronization.
In synchronization, there are two types of locks on threads:
- Object level lock : Every object in java has a unique lock. Whenever we are using synchronized keyword, then only lock concept will come in the picture. If a thread wants to execute synchronized method on the given object. First, it has to get lock of that object. Once thread got the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and release lock internally is taken care by JVM and programmer is not responsible for these activities. Lets have a look on the below program to understand the object level lock:
// Java program to illustrate// Object lock conceptclassGeekimplementsRunnable {publicvoidrun(){Lock();}publicvoidLock(){System.out.println(Thread.currentThread().getName());synchronized(this){System.out.println("in block "+ Thread.currentThread().getName());System.out.println("in block "+Thread.currentThread().getName() +" end");}}publicstaticvoidmain(String[] args){Geek g =newGeek();Thread t1 =newThread(g);Thread t2 =newThread(g);Geek g1 =newGeek();Thread t3 =newThread(g1);t1.setName("t1");t2.setName("t2");t3.setName("t3");t1.start();t2.start();t3.start();}}chevron_rightfilter_noneOutput:
t1 in block t1 in block t1 end t2 in block t2 in block t2 end t3 in block t3 in block t3 end
-
Class level lock : Every class in java has a unique lock which is nothing but class level lock. If a thread wants to execute a static synchronized method, then thread requires class level lock. Once a thread got the class level lock, then it is allowed to execute any static synchronized method of that class. Once method execution completes automatically thread releases the lock. Lets look on the below program for better understanding:
// Java program to illustrate class level lockclassGeekimplementsRunnable {publicvoidrun(){Lock();}publicvoidLock(){System.out.println(Thread.currentThread().getName());synchronized(Geek.class){System.out.println("in block "+ Thread.currentThread().getName());System.out.println("in block "+ Thread.currentThread().getName() +" end");}}publicstaticvoidmain(String[] args){Geek g1 =newGeek();Thread t1 =newThread(g1);Thread t2 =newThread(g1);Geek g2 =newGeek();Thread t3 =newThread(g2);t1.setName("t1");t2.setName("t2");t3.setName("t3");t1.start();t2.start();t3.start();}}chevron_rightfilter_noneOutput:
t1 in block t1 in block t1 end t2 in block t2 in block t2 end t3 in block t3 in block t3 end
Reference : https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Program to Create an Object for Class and Assign Value in the Object Using Constructor
- Does JVM create object of Main class (the class with main())?
- Collator compare(Object, Object) method in Java with Example
- Java | Class and Object | Question 1
- Java | Class and Object | Question 2
- Java | Class and Object | Question 4
- Java | Class and Object | Question 4
- Java | Class and Object | Question 5
- Java | Class and Object | Question 6
- Java | Class and Object | Question 7
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Java Object Creation of Inherited Class
- Object class in Java
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
Improved By : AyushBaheti

