Object level locking vs Class level locking in java
GIAN Tutorials
8:01 AM
Synchronization is ability to restrict access to shared resource to only one thread. When two or more threads need access to shared resource, there has to be some mechanism such that shared resource will be used by only one thread. The process by which we can achieve it is called Synchronization. Why do you need Synchronization? Let’s understand this with the help of example. Let’s say you want to count number of request you got for a particular URL. If you get two requests at the same time, then count may be inconsistent. Without Synchronization: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 package org . arpit . java2blog ; public class RequestCounter { private int count ; public int incrementCount ( ) { count ++ ; return count ; } } For example: Thread T1 sees count as 20 and increment it to 21. At the same time, thread t2 also sees count as 20 and increment it to