Java Object Level Synchronization

Object Level Synchronization comes into the picture when two or more using the same Object Or shared a resource across the multiple threads. Without synchronization there is a heavy chances of the data might go into inconsistent state.

Synchronization is the only way to avoid the data inconsistency of a Shared resource or an object, synchronization is a concept which prevents accessing the shared resource or an object by multiple threads at a time.

Synchronization allows only one thread can access the resource at a time. In Java synchronization can be achieved by using the keyword synchronized.

Every object in a Java possesses a lock, synchronized block or method allows to acquire a lock of an object by only one thread at a time. Once the thread is completed the execution of that synchronized block or method then the lock of that object will be released for other threads.

Object Level Synchronization in Java example

Output

<-PREV NEXT->