Saturday, 23 April 2016

What are Intrinsic locks and how they are used for synchronization in Java ?


Every Java object can implicitly acts as a lock for providing synchronization.
These built-in lock are called intrinsic locks / monitor locks and acts as mutexes / mutual exclusion locks in Java.


When thread A attempts to acquire a lock held by thread B, A must wait / block, until B releases it.

Java provide build-in locking mechanism using :
1. Synchronized block
    a. takes Object reference acts as a lock
    b. works on block of code

2. Synchronized method
    a. uses Object reference on which method is invoked as a lock
    b. spans entire method
   
Note : static + synchronized method uses Class object as the lock

Intrinsic locks are re-entrant , means same thread can acquire the lock again.

  - Java Concurrency In Practice

No comments:

Post a Comment

Note: only a member of this blog may post a comment.