This class is the starting context for performing directory operations. The documentation in the class description of InitialContext (including those for synchronization) apply here.
@author
Rosanna Lee
@author
Scott Seligman
@version
1.11 04/07/16
@since
1.3
Constructs an initial DirContext. No environment properties are supplied. Equivalent to new InitialDirContext(null).
Throws
NamingExceptionif a naming exception is encountered
Constructs an initial DirContext using the supplied environment. Environment properties are discussed in the javax.naming.InitialContext class description.

This constructor will not modify environment or save a reference to it, but may save a clone.

Parameters
environment environment used to create the initial DirContext. Null indicates an empty environment.
Throws
NamingExceptionif a naming exception is encountered
This constant specifies to add an attribute with the specified values.

If attribute does not exist, create the attribute. The resulting attribute has a union of the specified value set and the prior value set. Adding an attribute with no value will throw InvalidAttributeValueException if the attribute must have at least one value. For a single-valued attribute where that attribute already exists, throws AttributeInUseException. If attempting to add more than one value to a single-valued attribute, throws InvalidAttributeValueException.

The value of this constant is 1.

This constant specifies to delete the specified attribute values from the attribute.

The resulting attribute has the set difference of its prior value set and the specified value set. If no values are specified, deletes the entire attribute. If the attribute does not exist, or if some or all members of the specified value set do not exist, this absence may be ignored and the operation succeeds, or a NamingException may be thrown to indicate the absence. Removal of the last value will remove the attribute if the attribute is required to have at least one value.

The value of this constant is 3.

This constant specifies to replace an attribute with specified values.

If attribute already exists, replaces all existing values with new specified values. If the attribute does not exist, creates it. If no value is specified, deletes all the values of the attribute. Removal of the last value will remove the attribute if the attribute is required to have at least one value. If attempting to add more than one value to a single-valued attribute, throws InvalidAttributeValueException.

The value of this constant is 2.

Binds a name to an object, along with associated attributes. If attrs is null, the resulting binding will have the attributes associated with obj if obj is a DirContext, and no attributes otherwise. If attrs is non-null, the resulting binding will have attrs as its attributes; any attributes associated with obj are ignored.
Parameters
name the name to bind; may not be empty
obj the object to bind; possibly null
attrs the attributes to associate with the binding
Throws
NameAlreadyBoundExceptionif name is already bound
InvalidAttributesExceptionif some "mandatory" attributes of the binding are not supplied
NamingExceptionif a naming exception is encountered
Binds a name to an object, along with associated attributes. See for details.
Parameters
name the name to bind; may not be empty
obj the object to bind; possibly null
attrs the attributes to associate with the binding
Throws
NameAlreadyBoundExceptionif name is already bound
InvalidAttributesExceptionif some "mandatory" attributes of the binding are not supplied
NamingExceptionif a naming exception is encountered
Composes the name of this context with a name relative to this context. Since an initial context may never be named relative to any context other than itself, the value of the prefix parameter must be an empty name.
Composes the name of this context with a name relative to this context. Since an initial context may never be named relative to any context other than itself, the value of the prefix parameter must be an empty name ("").
Creates and binds a new context, along with associated attributes. This method creates a new subcontext with the given name, binds it in the target context (that named by all but terminal atomic component of the name), and associates the supplied attributes with the newly created object. All intermediate and target contexts must already exist. If attrs is null, this method is equivalent to Context.createSubcontext().
Parameters
name the name of the context to create; may not be empty
attrs the attributes to associate with the newly created context
Return
the newly created context
Throws
NameAlreadyBoundExceptionif the name is already bound
InvalidAttributesExceptionif attrs does not contain all the mandatory attributes required for creation
NamingExceptionif a naming exception is encountered
Creates and binds a new context, along with associated attributes. See for details.
Parameters
name the name of the context to create; may not be empty
attrs the attributes to associate with the newly created context
Return
the newly created context
Throws
NameAlreadyBoundExceptionif the name is already bound
InvalidAttributesExceptionif attrs does not contain all the mandatory attributes required for creation
NamingExceptionif a naming exception is encountered
Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Parameters
objthe reference object with which to compare.
Return
true if this object is the same as the obj argument; false otherwise.
Retrieves all of the attributes associated with a named object. See the class description regarding attribute models, attribute type names, and operational attributes.
Parameters
name the name of the object from which to retrieve attributes
Return
the set of attributes associated with name. Returns an empty attribute set if name has no attributes; never null.
Throws
NamingExceptionif a naming exception is encountered
Retrieves selected attributes associated with a named object. See the class description regarding attribute models, attribute type names, and operational attributes.

If the object does not have an attribute specified, the directory will ignore the nonexistent attribute and return those requested attributes that the object does have.

A directory might return more attributes than was requested (see Attribute Type Names in the class description), but is not allowed to return arbitrary, unrelated attributes.

See also Operational Attributes in the class description.

Parameters
name the name of the object from which to retrieve attributes
attrIds the identifiers of the attributes to retrieve. null indicates that all attributes should be retrieved; an empty array indicates that none should be retrieved.
Return
the requested attributes; never null
Throws
NamingExceptionif a naming exception is encountered
Retrieves all of the attributes associated with a named object. See for details.
Parameters
name the name of the object from which to retrieve attributes
Return
the set of attributes associated with name
Throws
NamingExceptionif a naming exception is encountered
Retrieves selected attributes associated with a named object. See for details.
Parameters
name The name of the object from which to retrieve attributes
attrIds the identifiers of the attributes to retrieve. null indicates that all attributes should be retrieved; an empty array indicates that none should be retrieved.
Return
the requested attributes; never null
Throws
NamingExceptionif a naming exception is encountered
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Return
The java.lang.Class object that represents the runtime class of the object. The result is of type {@code Class} where X is the erasure of the static type of the expression on which getClass is called.
Retrieves the schema associated with the named object. The schema describes rules regarding the structure of the namespace and the attributes stored within it. The schema specifies what types of objects can be added to the directory and where they can be added; what mandatory and optional attributes an object can have. The range of support for schemas is directory-specific.

This method returns the root of the schema information tree that is applicable to the named object. Several named objects (or even an entire directory) might share the same schema.

Issues such as structure and contents of the schema tree, permission to modify to the contents of the schema tree, and the effect of such modifications on the directory are dependent on the underlying directory.

Parameters
name the name of the object whose schema is to be retrieved
Return
the schema associated with the context; never null
Throws
OperationNotSupportedExceptionif schema not supported
NamingExceptionif a naming exception is encountered
Retrieves the schema associated with the named object. See for details.
Parameters
name the name of the object whose schema is to be retrieved
Return
the schema associated with the context; never null
Throws
OperationNotSupportedExceptionif schema not supported
NamingExceptionif a naming exception is encountered
Retrieves a context containing the schema objects of the named object's class definitions.

One category of information found in directory schemas is class definitions. An "object class" definition specifies the object's type and what attributes (mandatory and optional) the object must/can have. Note that the term "object class" being referred to here is in the directory sense rather than in the Java sense. For example, if the named object is a directory object of "Person" class, getSchemaClassDefinition() would return a DirContext representing the (directory's) object class definition of "Person".

The information that can be retrieved from an object class definition is directory-dependent.

Prior to JNDI 1.2, this method returned a single schema object representing the class definition of the named object. Since JNDI 1.2, this method returns a DirContext containing all of the named object's class definitions.

Parameters
name the name of the object whose object class definition is to be retrieved
Return
the DirContext containing the named object's class definitions; never null
Throws
OperationNotSupportedExceptionif schema not supported
NamingExceptionif a naming exception is encountered
Retrieves a context containing the schema objects of the named object's class definitions. See for details.
Parameters
name the name of the object whose object class definition is to be retrieved
Return
the DirContext containing the named object's class definitions; never null
Throws
OperationNotSupportedExceptionif schema not supported
NamingExceptionif a naming exception is encountered
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Return
a hash code value for this object.
Modifies the attributes associated with a named object. The order of the modifications is not specified. Where possible, the modifications are performed atomically.
Parameters
name the name of the object whose attributes will be updated
mod_op the modification operation, one of: ADD_ATTRIBUTE, REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE.
attrs the attributes to be used for the modification; may not be null
Throws
AttributeModificationExceptionif the modification cannot be completed successfully
NamingExceptionif a naming exception is encountered
Modifies the attributes associated with a named object using an ordered list of modifications. The modifications are performed in the order specified. Each modification specifies a modification operation code and an attribute on which to operate. Where possible, the modifications are performed atomically.
Parameters
name the name of the object whose attributes will be updated
mods an ordered sequence of modifications to be performed; may not be null
Throws
AttributeModificationExceptionif the modifications cannot be completed successfully
NamingExceptionif a naming exception is encountered
Modifies the attributes associated with a named object. See for details.
Parameters
name the name of the object whose attributes will be updated
mod_op the modification operation, one of: ADD_ATTRIBUTE, REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE.
attrs the attributes to be used for the modification; map not be null
Throws
AttributeModificationExceptionif the modification cannot be completed successfully
NamingExceptionif a naming exception is encountered
Modifies the attributes associated with a named object using an ordered list of modifications. See for details.
Parameters
name the name of the object whose attributes will be updated
mods an ordered sequence of modifications to be performed; may not be null
Throws
AttributeModificationExceptionif the modifications cannot be completed successfully
NamingExceptionif a naming exception is encountered
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

  • By executing a synchronized instance method of that object.
  • By executing the body of a synchronized statement that synchronizes on the object.
  • For objects of type Class, by executing a synchronized static method of that class.

Only one thread at a time can own an object's monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Binds a name to an object, along with associated attributes, overwriting any existing binding. If attrs is null and obj is a DirContext, the attributes from obj are used. If attrs is null and obj is not a DirContext, any existing attributes associated with the object already bound in the directory remain unchanged. If attrs is non-null, any existing attributes associated with the object already bound in the directory are removed and attrs is associated with the named object. If obj is a DirContext and attrs is non-null, the attributes of obj are ignored.
Parameters
name the name to bind; may not be empty
obj the object to bind; possibly null
attrs the attributes to associate with the binding
Throws
InvalidAttributesExceptionif some "mandatory" attributes of the binding are not supplied
NamingExceptionif a naming exception is encountered
Binds a name to an object, along with associated attributes, overwriting any existing binding. See for details.
Parameters
name the name to bind; may not be empty
obj the object to bind; possibly null
attrs the attributes to associate with the binding
Throws
InvalidAttributesExceptionif some "mandatory" attributes of the binding are not supplied
NamingExceptionif a naming exception is encountered
Searches in a single context for objects that contain a specified set of attributes. This method returns all the attributes of such objects. It is equivalent to supplying null as the atributesToReturn parameter to the method search(Name, Attributes, String[]).
See for a full description.
Parameters
name the name of the context to search
matchingAttributes the attributes to search for
Return
an enumeration of SearchResult objects
Throws
NamingExceptionif a naming exception is encountered
Searches in a single context for objects that contain a specified set of attributes, and retrieves selected attributes. The search is performed using the default SearchControls settings.

For an object to be selected, each attribute in matchingAttributes must match some attribute of the object. If matchingAttributes is empty or null, all objects in the target context are returned.

An attribute A1 in matchingAttributes is considered to match an attribute A2 of an object if A1 and A2 have the same identifier, and each value of A1 is equal to some value of A2. This implies that the order of values is not significant, and that A2 may contain "extra" values not found in A1 without affecting the comparison. It also implies that if A1 has no values, then testing for a match is equivalent to testing for the presence of an attribute A2 with the same identifier.

The precise definition of "equality" used in comparing attribute values is defined by the underlying directory service. It might use the Object.equals method, for example, or might use a schema to specify a different equality operation. For matching based on operations other than equality (such as substring comparison) use the version of the search method that takes a filter argument.

When changes are made to this DirContext, the effect on enumerations returned by prior calls to this method is undefined.

If the object does not have the attribute specified, the directory will ignore the nonexistent attribute and return the requested attributes that the object does have.

A directory might return more attributes than was requested (see Attribute Type Names in the class description), but is not allowed to return arbitrary, unrelated attributes.

See also Operational Attributes in the class description.

Parameters
name the name of the context to search
matchingAttributes the attributes to search for. If empty or null, all objects in the target context are returned.
attributesToReturn the attributes to return. null indicates that all attributes are to be returned; an empty array indicates that none are to be returned.
Return
a non-null enumeration of SearchResult objects. Each SearchResult contains the attributes identified by attributesToReturn and the name of the corresponding object, named relative to the context named by name.
Throws
NamingExceptionif a naming exception is encountered
Searches in the named context or object for entries that satisfy the given search filter. Performs the search as specified by the search controls.

The interpretation of filterExpr is based on RFC 2254. It may additionally contain variables of the form {i} -- where i is an integer -- that refer to objects in the filterArgs array. The interpretation of filterExpr is otherwise identical to that of the filter parameter of the method search(Name, String, SearchControls).

When a variable {i} appears in a search filter, it indicates that the filter argument filterArgs[i] is to be used in that place. Such variables may be used wherever an attr, value, or matchingrule production appears in the filter grammar of RFC 2254, section 4. When a string-valued filter argument is substituted for a variable, the filter is interpreted as if the string were given in place of the variable, with any characters having special significance within filters (such as '*') having been escaped according to the rules of RFC 2254.

For directories that do not use a string representation for some or all of their attributes, the filter argument corresponding to an attribute value may be of a type other than String. Directories that support unstructured binary-valued attributes, for example, should accept byte arrays as filter arguments. The interpretation (if any) of filter arguments of any other type is determined by the service provider for that directory, which maps the filter operations onto operations with corresponding semantics in the underlying directory.

This method returns an enumeration of the results. Each element in the enumeration contains the name of the object and other information about the object (see SearchResult). The name is either relative to the target context of the search (which is named by the name parameter), or it is a URL string. If the target context is included in the enumeration (as is possible when cons specifies a search scope of SearchControls.OBJECT_SCOPE or SearchControls.SUBSTREE_SCOPE), its name is the empty string.

The SearchResult may also contain attributes of the matching object if the cons argument specifies that attributes be returned.

If the object does not have a requested attribute, that nonexistent attribute will be ignored. Those requested attributes that the object does have will be returned.

A directory might return more attributes than were requested (see Attribute Type Names in the class description) but is not allowed to return arbitrary, unrelated attributes.

If a search filter with invalid variable substitutions is provided to this method, the result is undefined. When changes are made to this DirContext, the effect on enumerations returned by prior calls to this method is undefined.

See also Operational Attributes in the class description.

Parameters
name the name of the context or object to search
filterExpr the filter expression to use for the search. The expression may contain variables of the form "{i}" where i is a nonnegative integer. May not be null.
filterArgs the array of arguments to substitute for the variables in filterExpr. The value of filterArgs[i] will replace each occurrence of "{i}". If null, equivalent to an empty array.
cons the search controls that control the search. If null, the default search controls are used (equivalent to (new SearchControls())).
Return
an enumeration of SearchResults of the objects that satisfy the filter; never null
Throws
ArrayIndexOutOfBoundsExceptionif filterExpr contains {i} expressions where i is outside the bounds of the array filterArgs
InvalidSearchControlsExceptionif cons contains invalid settings
InvalidSearchFilterExceptionif filterExpr with filterArgs represents an invalid search filter
NamingExceptionif a naming exception is encountered
Searches in the named context or object for entries that satisfy the given search filter. Performs the search as specified by the search controls.

The format and interpretation of filter follows RFC 2254 with the following interpretations for attr and value mentioned in the RFC.

attr is the attribute's identifier.

value is the string representation the attribute's value. The translation of this string representation into the attribute's value is directory-specific.

For the assertion "someCount=127", for example, attr is "someCount" and value is "127". The provider determines, based on the attribute ID ("someCount") (and possibly its schema), that the attribute's value is an integer. It then parses the string "127" appropriately.

Any non-ASCII characters in the filter string should be represented by the appropriate Java (Unicode) characters, and not encoded as UTF-8 octets. Alternately, the "backslash-hexcode" notation described in RFC 2254 may be used.

If the directory does not support a string representation of some or all of its attributes, the form of search that accepts filter arguments in the form of Objects can be used instead. The service provider for such a directory would then translate the filter arguments to its service-specific representation for filter evaluation. See search(Name, String, Object[], SearchControls).

RFC 2254 defines certain operators for the filter, including substring matches, equality, approximate match, greater than, less than. These operators are mapped to operators with corresponding semantics in the underlying directory. For example, for the equals operator, suppose the directory has a matching rule defining "equality" of the attributes in the filter. This rule would be used for checking equality of the attributes specified in the filter with the attributes of objects in the directory. Similarly, if the directory has a matching rule for ordering, this rule would be used for making "greater than" and "less than" comparisons.

Not all of the operators defined in RFC 2254 are applicable to all attributes. When an operator is not applicable, the exception InvalidSearchFilterException is thrown.

The result is returned in an enumeration of SearchResults. Each SearchResult contains the name of the object and other information about the object (see SearchResult). The name is either relative to the target context of the search (which is named by the name parameter), or it is a URL string. If the target context is included in the enumeration (as is possible when cons specifies a search scope of SearchControls.OBJECT_SCOPE or SearchControls.SUBSTREE_SCOPE), its name is the empty string. The SearchResult may also contain attributes of the matching object if the cons argument specified that attributes be returned.

If the object does not have a requested attribute, that nonexistent attribute will be ignored. Those requested attributes that the object does have will be returned.

A directory might return more attributes than were requested (see Attribute Type Names in the class description) but is not allowed to return arbitrary, unrelated attributes.

See also Operational Attributes in the class description.

Parameters
name the name of the context or object to search
filter the filter expression to use for the search; may not be null
cons the search controls that control the search. If null, the default search controls are used (equivalent to (new SearchControls())).
Return
an enumeration of SearchResults of the objects that satisfy the filter; never null
Throws
InvalidSearchFilterExceptionif the search filter specified is not supported or understood by the underlying directory
InvalidSearchControlsExceptionif the search controls contain invalid settings
NamingExceptionif a naming exception is encountered
Searches in a single context for objects that contain a specified set of attributes. See for details.
Parameters
name the name of the context to search
matchingAttributes the attributes to search for
Return
an enumeration of SearchResult objects
Throws
NamingExceptionif a naming exception is encountered
Searches in a single context for objects that contain a specified set of attributes, and retrieves selected attributes. See for details.
Parameters
name the name of the context to search
matchingAttributes the attributes to search for
attributesToReturn the attributes to return
Return
a non-null enumeration of SearchResult objects
Throws
NamingExceptionif a naming exception is encountered
Searches in the named context or object for entries that satisfy the given search filter. Performs the search as specified by the search controls. See for details.
Parameters
name the name of the context or object to search
filterExpr the filter expression to use for the search. The expression may contain variables of the form "{i}" where i is a nonnegative integer. May not be null.
filterArgs the array of arguments to substitute for the variables in filterExpr. The value of filterArgs[i] will replace each occurrence of "{i}". If null, equivalent to an empty array.
cons the search controls that control the search. If null, the default search controls are used (equivalent to (new SearchControls())).
Return
an enumeration of SearchResults of the objects that satisfy the filter; never null
Throws
ArrayIndexOutOfBoundsExceptionif filterExpr contains {i} expressions where i is outside the bounds of the array filterArgs
InvalidSearchControlsExceptionif cons contains invalid settings
InvalidSearchFilterExceptionif filterExpr with filterArgs represents an invalid search filter
NamingExceptionif a naming exception is encountered
Searches in the named context or object for entries that satisfy the given search filter. Performs the search as specified by the search controls. See for details.
Parameters
name the name of the context or object to search
filter the filter expression to use for the search; may not be null
cons the search controls that control the search. If null, the default search controls are used (equivalent to (new SearchControls())).
Return
an enumeration of SearchResults for the objects that satisfy the filter.
Throws
InvalidSearchFilterExceptionif the search filter specified is not supported or understood by the underlying directory
InvalidSearchControlsExceptionif the search controls contain invalid settings
NamingExceptionif a naming exception is encountered
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 
Return
a string representation of the object.
Causes current thread to wait until another thread invokes the method or the method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Throws
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.

The current thread must own this object's monitor.

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

  • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
  • Some other thread invokes the notifyAll method for this object.
  • Some other thread interrupts thread T.
  • The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout);
         ... // Perform action appropriate to condition
     }
 
(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).

If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Parameters
timeoutthe maximum time to wait in milliseconds.
Throws
IllegalArgumentExceptionif the value of timeout is negative.
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

This method is similar to the wait method of one argument, but it allows finer control over the amount of time to wait for a notification before giving up. The amount of real time, measured in nanoseconds, is given by:

 1000000*timeout+nanos

In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:

  • Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
  • The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.

The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout, nanos);
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Parameters
timeoutthe maximum time to wait in milliseconds.
nanosadditional time, in nanoseconds range 0-999999.
Throws
IllegalArgumentExceptionif the value of timeout is negative or the value of nanos is not in the range 0-999999.
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.