简体   繁体   中英

What's the meaning of timeout if wait(timeout) and notifyAll() are locked by the same object

For the following code notifyAll() will hold the lock until done, even when the timeout has reached, this block doesn't hold the lock and has to wait for the notifyAll() block to be finished. Then what's the meaning of the timeout in wait(timeout) if after the timeout is done we still have to wait for the lock? Also - how to change the code so that the timeout will have meaning?

// one thread
synchronized (lock) {
  lock.wait(timeout);
}

// second thread
synchronized (lock) {
  // do some processing actions.......
  lock.notifyAll();
}

You are indeed correct that the waiting thread actually goes through 2 types of wait: wait for an explicit 'notify/notifyAll', and then wait for an opportunity to get the synchronization lock.

The hope is, that most other threads that use 'synchronized' will only hold the synchronization lock for a brief time. That's a very strongly recommended practice. A private case of it is the thread that calls 'notifyAll' - this is a very short action , and the synchronization block is existed very quickly.

To summarize: the thread might be stuck on 'lock.wait' for a long time (eg "waiting for a customer to arrive" - this could take hours, and you might consider a timeout after which you despair of business). However, once the notification arrives and it competes on 'synchronized' - this competition should be brief, so brief that it's not worth it to consider timeout. However, this relies on the good will of your fellow programmers, that should only use synchronized for short blocks (eg avoiding a race condition in that fraction of a second when you're updating a variable). It's a matter of good practice.

The wait's timeout has a good meaning. Just read and JavaDoc and think about it. Not sure why you think it has no meaning here, I guess you just got confused.

Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

The current thread must own this object's monitor.

Object.wait(long)

There are situations, where lock.wait() waits for a resource, which may never be available ( lock.notify() is never called). For example, responce from the remote computer will never be recieved if it is crashed(or network is crashed).

One choice in these situation is to wait forever, allowing user to interrupt waiting by hands.

Another choice is to use lock.wait(timeout) to wait for a limited amount of time, assuming resource as inaccessible after that amount is expired. In that case(after timeout is expired) program can choose another way to complete task. Or program can simply exit and allow other programs to do their work.

Without taking spurious wakeups into account, usage is simple:

if(!condition)
{
    lock.wait(timeout);
    if(!condition)
    {
        //timeout expired while waiting
    }
}
// 'condition' is true now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM