monitorexit

Exit monitor for object

Bytecode

Type Description
u1 monitorexit opcode = 0xC3 (195)

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.

This releases an exclusive lock that was previously obtained using monitorenter for the object objectref. If other threads are waiting for the object to become unlocked, one of the waiting threads will be able to acquire a lock on the object and proceed.

Note that a single thread can lock an object several times - the runtime system maintains a count of the number of times that the object was locked by the current thread, and only unlocks the object when the counter reaches zero (i.e. when the number of monitorenters and monitorexits applied to a given object are equal).

The monitorenter/monitorexit mechanism is used by the Java synchronized statement to coordinate access to an object among multiple threads.

Exceptioms

NullPointerException - objectref is null.

IllegalMonitorStateException - objectref was not previously locked by the current thread.