---
title: Limitations of intrinsic lock and solution
description: Limitations of intrinsic lock and solution
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [News](https://blog.e-zest.com/news)
- [Podcasts](https://www.e-zest.com/podcasts)
- [Healthcare](https://blog.e-zest.com/healthcare)
- [CSR](https://blog.e-zest.com/csr)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [Media Kit](https://www.e-zest.com/media-kit)

# Limitations of intrinsic lock and solution

[Madhura Oak](https://blog.e-zest.com/author/madhura-oak)  Jun 04, 2012

[ReentrantLock](https://blog.e-zest.com/topic/reentrantlock) [ReentrantReadWriteLock](https://blog.e-zest.com/topic/reentrantreadwritelock) [lock](https://blog.e-zest.com/topic/lock) [fairness](https://blog.e-zest.com/topic/fairness) [Java](https://blog.e-zest.com/topic/java) [ReadWriteLock](https://blog.e-zest.com/topic/readwritelock) [Lock API](https://blog.e-zest.com/topic/lock-api) [intrinsic lock](https://blog.e-zest.com/topic/intrinsic-lock) [Multithreading](https://blog.e-zest.com/topic/multithreading) [monitor lock](https://blog.e-zest.com/topic/monitor-lock) [Technology](https://blog.e-zest.com/topic/technology)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/&text=Limitations%20of%20intrinsic%20lock%20and%20solution>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/>

Java SE 5.0 introduced classes for locking called as Lock API. They are part of *java.util.concurrent.locks* package. Until then, only synchronized methods and synchronized blocks were available for locking.

A built-in lock called *intrinstic lock* or *monitor lock* is obtained on the object which is locked by synchronized method or block. This lock is acquired by the thread executing synchronized method/block and released when the thread leaves it. The intrinsic lock is released under the following scenarios:

- the synchronized method/block execution is completed normally
- there is an exception thrown which is not handled within the synchronized method/block
- `wait()` is called in the synchronized method/block

**Limitations of intrinsic lock**

The intrinstic lock acts as a *mutex* (mutally exclusive lock) where only one thread can run in the object’s monitor until the lock is released. There cannot be more than one owning thread in an intrinsic lock.

The intrinsic locks are mutually exclusive locks. In an object, we may have a property which can be read or modified concurrently by multiple threads. The threads which are reading the property cannot obtain a shared lock.

When an intrinsic lock is acquired by a thread on an object the other threads trying to access the object’s monitor will have to keep waiting until the executing thread releases the lock and leaves the object’s monitor. When a thread leaves the object’s monitor, the JVM can grant its lock to any other thread waiting for its access and not necessarily the oldest thread in the waiting state. In other words, there is no fairness guarentee on lock acquisition when synchronization is used. We can also say that intrinsic locks are unfair. The JVM guarentees that all threads waiting for access to the object’s monitor will be granted access before its shutdown but it does not guarentee the order of locking. Thus, intrinsic locks could lead to thread starvation.

There is no non-blocking way to obtain a lock using synchronization. The thread which wants to obtain a lock will have to keep waiting if the lock is being held by other thread. The requesting thread cannot check whether a lock is available and perform other operations if its unavailable.

When a thread acquires an intrinsic lock, it cannot be interrupted or the thread’s task cannot be cancelled before the thread leaves the intrinsic lock.

There is no way to obtain the collection of threads waiting to acquire the lock. We also cannot determine the number of threads waiting to acquire the lock.

**Lock API**

To overcome the limitations of intrinsic lock you can use the Lock API. This API provides `Lock, ReadWriteLock` and `Condition` interfaces and their implementing classes.

The `wait()` allows only one thread to own the object’s monitor. If you need to use multiple *wait*-sets in a single object then you will have to use the `Condition` object provided by the `Lock` factory method `newCondition()`. Read this example of [bounded buffer implementation using multiple conditions](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/Condition.html).

The `ReadWriteLock` implementing classes can be used by a Publisher-Subscriber framework where the publisher thread modifies a property and subscriber threads read it. Shared locks can be held by a subscriber threads instead of the mutually exclusive intrinsic lock for better concurrency. The `ReentrantReadWriteLock` is the implementing class for `ReadWriteLock`.

`ReentrantLock` is an implementation of `Lock` which provides reentrant mutually exclusive lock. If you want to grant locks to the threads in the same order they asked for it then you can create a fair lock. A fair lock can be created by using `new ReentrantLock(true)`. However, use fair locks only when required as they reduce throughput in comparison with the unfair locks.

The `Lock` interface provides a `tryLock()` method with which a lock can be obtained in a non-blocking way. A thread can attempt to obtain a lock and it will be granted if it is not being held by any other thread. The thread requesting for a lock need not keep waiting for the lock. This is also called as *polled* lock. The lock obtained using `tryLock()` is unfair. You can use the alternate `tryLock(0, TimeUnit.SECONDS)` method to obtain a fair lock if the *fairness* is set to true using the `ReentrantLock` constructor. In case of unfair locking, the current thread requesting for a lock using `tryLock()` will acquire the lock even if other threads are waiting for the lock. In the case of fair locking, the current thread will not acquire the lock if other threads are waiting for it. This method returns `true` if the thread obtains the lock on object’s monitor.

There is also a *timed* `tryLock` method to specify the time limit for which a thread can wait to obtain the lock. This method considers the fairness setting.

The `Lock` interface provides `lockInterruptibly()` method with which the lock obtained by the executing thread will be released when the thread is interrupted. It allows a thread to stop execution and release the lock before completion of its task when interrupted.

The `getQueuedThreads()` and `getQueueLength()` methods of ReentrantLock can be used to get the collection of threads waiting to acquire the lock and to get the number of threads waiting to acquire the lock respectively.

**Performance comparison of ReentrantLock and synchronized**

The `ReentrantLock` provides same semantics as synchronized method/block. In Java 5 JVM, the `ReentrantLock` has better performance than synchronized under thread contention. However, in Java 6 there is no significant difference in the performance of `ReentrantLock` and synchronized.

**When should we use the Lock API?**

The major drawback of `ReentrantLock, ReentrantReadWriteLock.ReadLock` and `ReentrantReadWriteLock.WriteLock` classes is that the lock needs to be explicitly released within the `finally` block. A programmer may forget to release the lock when its use is over. The Lock API classes should be used only when you need to overcome the limitations of the built-in synchronized method/block. Only expert programmers should write the code which use these classes.

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/&text=Limitations%20of%20intrinsic%20lock%20and%20solution>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/limitations-of-intrinsic-lock-and-solution/&title=&summary=&source=>

### Similar Blog

[Interrupting Threads In Java In a Java application, a thread may keep waiting for a long time after executing `wait()` or `Thread.join()` or by `put()` or `take()` ... By Madhura Oak Sep 04, 2012](https://blog.e-zest.com/interrupting-threads-in-java/)

[Oracle NoSQL Database (ONDB) Relational databases have been around us for more than 20 years now. Needless to say, they are mature and an obvious choice for ... By Madhura Oak Dec 24, 2013](https://blog.e-zest.com/oracle-nosql-database-ondb/)

[Session State Isolation Techniques Isolation of data in a session is the trickiest part of session management. A session may constitute of multiple requests. ... By Madhura Oak Dec 02, 2013](https://blog.e-zest.com/session-state-isolation-techniques/)

[Session Management There are three ways of session management in n-tier applications: By Madhura Oak Nov 27, 2013](https://blog.e-zest.com/session-management/)

[Transaction and Query timeout Performance is one of the non-functional requirements of an application. It defines the acceptable maximum response time for ... By Madhura Oak Nov 25, 2013](https://blog.e-zest.com/transaction-and-query-timeout/)

[![Multiple Inheritance in Domain Model Design](https://blog.e-zest.com/hs-fs/file-3586010320-png/blog-files/uml-class-diagram-%E2%80%93-bridge-pattern.png) Multiple Inheritance in Domain Model Design While using multiple inheritance in domain model design you need to be cautious as programming languages such as Java do not ... By Madhura Oak Nov 13, 2013](https://blog.e-zest.com/multiple-inheritance-in-domain-model-design/)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.