Contents | Prev | Next | IndexThe JavaTM Virtual Machine Specification


monitorenter

Operation

monitorenter


Enter monitor for object

Forms monitorenter = 194 (0xc2)

Stack ..., objectref ...

Description The objectref must be of type reference.

Each object has a monitor associated with it. The thread that executes monitorenter gains ownership of the monitor associated with objectref. If another thread already owns the monitor associated with objectref, the current thread waits until the object is unlocked, then tries again to gain ownership. If the current thread already owns the monitor associated with objectref, it increments a counter in the monitor indicating the number of times this thread has entered the monitor. If the monitor associated with objectref is not owned by any thread, the current thread becomes the owner of the monitor, setting the entry count of this monitor to 1.

Runtime Exception If objectref is null, monitorenter throws a NullPointerException.

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

The monitorenter instruction may be used with a monitorexit instruction to implement a Java synchronized block. The monitorenter instruction is not used in the implementation of synchronized methods, although it provides equivalent semantics; monitor entry on invocation of a synchronized method is handled implicitly by the Java Virtual Machine's method invocation instructions. See §7.14, in "Compiling for the Java Virtual Machine," for more information on the use of the monitorenter and monitorexit instructions.

The association of a monitor with an object may be managed in various ways that are beyond the scope of this specification. For instance, the monitor may be allocated and deallocated at the same time as the object. Alternatively, it may be dynamically allocated at the time when a thread attempts to gain exclusive access to the object and freed at some later time when no thread remains in the monitor for the object.

The synchronization constructs of the Java Language require support for operations on monitors besides entry and exit, including waiting on a monitor (Object.wait) and notifying other threads waiting in a monitor (Object.notify and Object.notifyAll). These operations are supported in the standard package java.lang, supplied with the Java Virtual Machine. No explicit support for these operations appears in the instruction set of the Java Virtual Machine.


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.


multianewarray

Operation Create new multidimensional array

multianewarray
indexbyte1
indexbyte2
dimensions


Forms multianewarray = 197 (0xc5)

Stack ..., count1, [count2, ...] ..., arrayref

Description The dimensions is an unsigned byte which must be greater than or equal to 1. It represents the number of dimensions of the array to be created. The operand stack must contain dimensions words, which must be of type int and nonnegative, each representing the number of components in a dimension of the array to be created. The count1 is the desired length in the first dimension, count2 in the second, etc.

All of the count values are popped off the operand stack. The unsigned indexbyte1 and indexbyte2 are used to construct an index into the constant pool of the current class (§3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The item at that index in the constant pool must be a CONSTANT_Class (§4.4.1). The symbolic reference is resolved (§5.1.3). The resulting entry must be an array class type of dimensionality greater than or equal to dimensions.

A new multidimensional array of the array type is allocated from the garbage-collected heap. The components of the array of in the first dimension are initialized to subarrays of the type of the second dimension, and so on. The components of the first dimension of the array are initialized to the default initial value for the type of the components (§2.5.1). A reference arrayref to the new array is pushed onto the operand stack.

Linking Exceptions During resolution of the CONSTANT_Class constant pool item, any of the exceptions documented in §5.1 can be thrown.

Otherwise, if the current class does not have permission to access the base class of the resolved array class, multianewarray throws an IllegalAccessError.

Runtime Exception Otherwise, if any of the dimensions values on the operand stack is less than zero, the multianewarray instruction throws a NegativeArraySizeException.

Notes It may be more efficient to use newarray or anewarray when creating an array of a single dimension.

The array class referenced via the constant pool instruction may have more dimensions than the dimensions operand of the multianewarray instruction. In that case, only the first dimensions of the dimensions of the array are created.


Contents | Prev | Next | Index

Java Virtual Machine Specification

Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to jvm@java.sun.com