monitorexit

Operation

monitorexit
Exit monitor for object

Forms
monitorexit = 195 (0xc3)

Stack
..., objectref ...

Description
The objectref must be of type reference.

The current thread must be the owner of the monitor associated with the instance referenced by objectref. The thread decrements the counter indicating the number of times it has entered this monitor. If as a result the value of the counter becomes zero, the current thread releases the monitor. If the monitor associated with objectref becomes free, other threads that are waiting to acquire that monitor are allowed to attempt to do so.

Runtime Exceptions
If objectref is null, monitorexit throws a NullPointerException.

Otherwise, if the current thread is not the owner of the monitor, monitorexit throws an IllegalMonitorStateException.

Notes
For detailed information about threads and monitors in the Java Virtual Machine, see Chapter 8, "Threads and Locks."

The monitorenter and monitorexit instructions may be used to implement Java's synchronized blocks. The monitorexit instruction is not used in the implementation of synchronized methods, although it provide equivalent semantics; monitor exit on normal or abnormal synchronized method completion is handled implicitly by the Java Virtual Machine's method invocation instructions. The Java Virtual Machine also implicitly handles monitor exit from within a synchronized block when an error is thrown. See §7.14, in "Compiling for the Java Virtual Machine," for more information on the use of the monitorenter and monitorexit instructions.