driftwood
driftwood

Reputation: 2111

What is happening when calling wait() method

I read in a Java textbook the following pertaining to multi-threading.

For a thread to call wait() or notify(), the thread has to be the owner of the lock for that object. When the thread waits, it temporarily releases the lock for other threads to use, but it will need it again to continue execution.

I'm confused about what is meant by the clause

When the thread waits, it temporarily releases the lock for other threads to use

I don't get what that clause is talking about. Is it saying that when the wait() method is called it is actually releasing the lock before the wait() returns (i.e. this happens without caller knowing)? Or is it just alluding to wait(timeout) releasing the lock when the timeout elapses? If it is the former why would it release the lock before notify()? This seems like a vague and poorly explained statement.

Upvotes: 3

Views: 1453

Answers (3)

Talendar
Talendar

Reputation: 2087

When a thread calls wait(), it's temporarily releasing the monitor (lock) of the object until it receives a notification from another thread. This way, a thread can willingly give control (that it has, in the first place) of the object's monitor to another thread. Take a look at the docs:

The invocation of wait() does not return until another thread has issued a notification that some special event may have occurred — though not necessarily the event this thread is waiting for (so always invoke wait() inside a loop that tests for the condition being waited for).

...

When wait() is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened.

Upvotes: 1

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13525

For a thread to call wait() or notify(), the thread has to be the owner of the lock for that object.

Otherwise, a runtime error occur and the rest of code is not executed.

When the thread waits, it temporarily releases the lock for other threads to use

In more details, call to wait() does the following:

  • the lock is released
  • current thread is registered as waiting in the monitor
  • processor switches to some other thread ready for execution

Then, some thread calls notify() or notifyAll(), which causes one or all threads which are registered as waiting at this monitor to be moved from the wait set to the ready set, waiting for a free processor to execute.

but it will need it again to continue execution.

This means the execution of the thread is continued with executing synchronized statement to regain the lock. After the lock is aquired, then the wait() method returns. wait(timeout) differs in that except for notify() or notifyAll(), it also can return upon the timeout.

In sum, you need to understand how a thread switches between following 4 states:

  • running on a processor
  • blocked on synchronized statement
  • waiting for notification
  • ready to execute and waiting for a free processor

Upvotes: 5

Nathan Hughes
Nathan Hughes

Reputation: 96385

When a thread calls wait, the thread releases the lock right away and then goes dormant until either the timeout expires, if any, or until it receives a notification, which occurs when another thread acquires the same lock that the waiting thread gave up and calls notify on it (also the scheduler has to pick the waiting thread from among any other waiting threads; calling notify doesn’t notify a given thread, it tells the scheduler to pick a thread from a given lock’s wait set to notify).

Once the thread is woken up by a notify, it has to reacquire the lock in order to leave the wait method, because the thread is still inside of a synchronized method or block. That is what the quote means when it says the thread will need the lock to resume execution.

Upvotes: 1

Related Questions