# monitor-exit

## Purpose

Release the monitor for the indicated object.

Note: If this instruction needs to throw an exception, it must do so as if the pc has already advanced past the instruction. It may be useful to think of this as the instruction successfully executing (in a sense), and the exception getting thrown after the instruction but before the next one gets a chance to run. This definition makes it possible for a method to use a monitor cleanup catch-all (e.g., finally) block as the monitor cleanup for that block itself, as a way to handle the arbitrary exceptions that might get thrown due to the historical implementation of Thread.stop(), while still managing to have proper monitor hygiene.

## Details

Op & Format | Mnemonic / Syntax | Arguments
--- | --- | ---
1e 11x | monitor-exit vAA | `A:` reference-bearing register (8 bits)

## Constraints

- A must be a valid register index for the current stack frame.
- Register vA must contain a reference to an object.

## Behavior

- An attempt is made for the current thread to release the monitor of the indicated object.
- If the current thread is the owner, the following happens:
  - The monitor's entry count is decreased by one.
  - If the entry count has reached zero, the monitor is released. Other threads waiting for the same monitor have a chance to acquire it.
- Any exception that gets thrown by this instruction bears the PC of the instruction following the monitor-exit. That is, from the point of view of an exception handler it cannot be distinguished from the same type of exception being thrown immediately after the monitor-exit instruction.

## Exceptions

- NullPointerException is thrown if vA is null.
- IllegalMonitorStateException is thrown if the current thread is not the owner of that monitor.