Udostępnij za pośrednictwem


Object.Wait Method

Definition

Overloads

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

[Android.Runtime.Register("wait", "()V", "")]
public void Wait ();
[<Android.Runtime.Register("wait", "()V", "")>]
member this.Wait : unit -> unit
Attributes

Exceptions

if the thread calling this method is not the owner of this object's monitor.

if the current thread has been interrupted. The interrupted status of the current thread will be cleared before the exception is thrown.

Remarks

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

In all respects, this method behaves as if wait(0L, 0) had been called. See the specification of the #wait(long, int) method for details.

Java documentation for java.lang.Object.wait().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to

Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

[Android.Runtime.Register("wait", "(JI)V", "")]
public void Wait (long timeoutMillis, int nanos);
[<Android.Runtime.Register("wait", "(JI)V", "")>]
member this.Wait : int64 * int -> unit

Parameters

timeoutMillis
Int64

the maximum time to wait, in milliseconds

nanos
Int32

additional time, in nanoseconds, in the range range 0-999999 inclusive

Attributes

Exceptions

if millis , nanos or nanos > 999999.

if the thread calling this method is not the owner of this object's monitor.

if the current thread has been interrupted. The interrupted status of the current thread will be cleared before the exception is thrown.

Remarks

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

The current thread must own this object's monitor lock. See the #notify notify method for a description of the ways in which a thread can become the owner of a monitor lock.

This method causes the current thread (referred to here as <var>T</var>) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Note that only the locks on this object are relinquished; any other objects on which the current thread may be synchronized remain locked while the thread waits.

Thread <var>T</var> then becomes disabled for thread scheduling purposes and lies dormant until one of the following occurs: <ul> <li>Some other thread invokes the notify method for this object and thread <var>T</var> happens to be arbitrarily chosen as the thread to be awakened. <li>Some other thread invokes the notifyAll method for this object. <li>Some other thread Thread#interrupt() interrupts thread <var>T</var>. <li>The specified amount of real time has elapsed, more or less. The amount of real time, in nanoseconds, is given by the expression 1000000 * timeoutMillis + nanos. If timeoutMillis and nanos are both zero, then real time is not taken into consideration and the thread waits until awakened by one of the other causes. <li>Thread <var>T</var> is awakened spuriously. (See below.) </ul>

The thread <var>T</var> is then removed from the wait set for this object and re-enabled for thread scheduling. It competes in the usual manner with other threads for the right to synchronize on the object; once it has regained 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 <var>T</var> 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 wake up without being notified, interrupted, or timing out, a so-called <em>spurious wakeup</em>. 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. See the example below.

For more information on this topic, see section 14.2, "Condition Queues," in Brian Goetz and others' <em>Java Concurrency in Practice</em> (Addison-Wesley, 2006) or Item 69 in Joshua Bloch's <em>Effective Java, Second Edition</em> (Addison-Wesley, 2008).

If the current thread is java.lang.Thread#interrupt() interrupted by any thread before or while it is waiting, then an InterruptedException is thrown. The <em>interrupted status</em> of the current thread is cleared when this exception is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

Java documentation for java.lang.Object.wait(long, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to

Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

[Android.Runtime.Register("wait", "(J)V", "")]
public void Wait (long timeoutMillis);
[<Android.Runtime.Register("wait", "(J)V", "")>]
member this.Wait : int64 -> unit

Parameters

timeoutMillis
Int64

the maximum time to wait, in milliseconds

Attributes

Exceptions

if the thread calling this method is not the owner of this object's monitor.

if the current thread has been interrupted. The interrupted status of the current thread will be cleared before the exception is thrown.

Remarks

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

In all respects, this method behaves as if wait(timeoutMillis, 0) had been called. See the specification of the #wait(long, int) method for details.

Java documentation for java.lang.Object.wait(long).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to