---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 6. The Java Virtual Machine Instruction Set"
section: "6.5.monitorexit"
heading: "monitorexit"
parent: "6.5. Instructions"
source: https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-6.html#jvms-6.5.monitorexit
id: "6.5.monitorexit"
url: https://jvm.obfuscated.software/jvms/se17/6.5.monitorexit.html
raw: https://jvm.obfuscated.software/jvms/se17/6.5.monitorexit.md
---

# monitorexit

## Operation

Exit monitor for object

## Format

```
monitorexit
```

## Forms

*monitorexit* = 195 (0xc3)

## Operand Stack

..., *objectref* →

...

## Description

The *objectref* must be of type `reference`.

The thread that executes *monitorexit* must be the owner of the monitor associated with the instance referenced by *objectref*.

The thread decrements the entry count of the monitor associated with *objectref*. If as a result the value of the entry count is zero, the thread exits the monitor and is no longer its owner. Other threads that are blocking to enter the monitor are allowed to attempt to do so.

## Run-time Exceptions

If *objectref* is `null`, *monitorexit* throws a `NullPointerException`.

Otherwise, if the thread that executes *monitorexit* is not the owner of the monitor associated with the instance referenced by *objectref*, *monitorexit* throws an `IllegalMonitorStateException`.

Otherwise, if the Java Virtual Machine implementation enforces the rules on structured locking described in [§2.11.10](2.11.10.md) and if the second of those rules is violated by the execution of this *monitorexit* instruction, then *monitorexit* throws an `IllegalMonitorStateException`.

## Notes

One or more *monitorexit* instructions may be used with a *monitorenter* instruction ([§*monitorenter*](6.5.monitorenter.md)) to implement a `synchronized` statement in the Java programming language ([§3.14](3.14.md)). The *monitorenter* and *monitorexit* instructions are not used in the implementation of `synchronized` methods, although they can be used to provide equivalent locking semantics.

The Java Virtual Machine supports exceptions thrown within `synchronized` methods and `synchronized` statements differently:

- Monitor exit on normal `synchronized` method completion is handled by the Java Virtual Machine's return instructions. Monitor exit on abrupt `synchronized` method completion is handled implicitly by the Java Virtual Machine's *athrow* instruction.
- When an exception is thrown from within a `synchronized` statement, exit from the monitor entered prior to the execution of the `synchronized` statement is achieved using the Java Virtual Machine's exception handling mechanism ([§3.14](3.14.md)).
