Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 101

The Object.wait() method temporarily cedes possession of a lock so that other threads that may be requesting the lock can proceed. Object.wait() must always be called from a synchronized block or method. The waiting thread resumes execution only after it has been notified, generally as the result of the invocation of the notify() or notifyAll() method by some other thread. The wait() method must be invoked from a loop that checks whether a condition predicate holds. Note that a condition predicate is the negation of the condition expression in the loop. For example, the condition predicate for removing an element from a vector is !isEmpty(), whereas the condition expression for the while loop condition is isEmpty(). Following is the correct way to invoke the wait() method when the vector is empty.

Code Block
bgColor#ccccff
langc
struct node_t {
  void* node;
  struct node_t* next;
};

struct node_t list;
static mtx_t lock;
static cnd_t condition;
 
void consume_list_element() {
  int result;
  if ((result = mtx_lock(&lock)) != thrd_success) {
    /* handle error */
  }

  while (list.next == NULL
private Vector vector;
//...

public void consumeElement() throws InterruptedException {
  synchronized (vector) {
    while (vector.isEmpty()) {
    if ((result = cnd_wait(&condition, &lock)) != thrd_success) { vector.wait();
      /* handle error */
    }
  }
 
  /*}

    // Resume when condition holds */

  if ((result = mtx_unlock(&lock)) != thrd_success) {
    /* handle error */
  }
}}
}

The notification mechanism notifies the waiting thread and allows it to check its condition predicate. The invocation of notify() or notifyAll() in another thread cannot precisely determine which waiting thread will be resumed. Condition predicate statements allow notified threads to determine whether they should resume upon receiving the notification. Condition predicates are also useful when a thread is required to block until a condition becomes true, for example, when waiting for data to arrive on an input stream before reading the data.

...

This noncompliant code example invokes the wait() method inside a traditional if block and fails to check the postcondition after the notification is received. If the notification were accidental or malicious, the thread could wake up prematurely.

Code Block
bgColor#FFcccc

synchronized (object) {
  if (<condition does not hold>) {
    object.wait();
  }
  // Proceed when condition holds
}

...

This compliant solution calls the wait() method from within a while loop to check the condition both before and after the call to wait().

Code Block
bgColor#ccccff

synchronized (object) {
  while (<condition does not hold>) {
    object.wait();
  }
  // Proceed when condition holds
}

...

[API 2006]

Class Object

[Bloch 2001]

Item 50. Never invoke wait outside a loop

[Lea 2000]

3.2.2, Monitor Mechanics; 1.3.2, Liveness

[Goetz 2006]

Section 14.2, Using Condition Queues

...

...

THI02-J. Notify all waiting threads rather than a single thread      09. Thread APIs (THI)