This discussion is archived
2 Replies Latest reply: Apr 23, 2013 11:54 AM by baftos RSS

Question on wait(timeout)?

909741 Newbie
Currently Being Moderated
hi,

The wait(timeout) method will try to reacquire the lock once the timeout has expired, up to the timeout it will release the lock. I hope it is correct.

If it is so, what will happen if the another thread has holds the lock before the timeout expires, and keeps the lock after the timeout expires also,

then what state the wait timeout thread will go? Will it keeps on waiting for lock like other threads?

Thanks in advance
  • 1. Re: Question on wait(timeout)?
    EJP Guru
    Currently Being Moderated
    The wait(timeout) method will try to reacquire the lock once the timeout has expired, up to the timeout it will release the lock. I hope it is correct.
    No. It will release the lock; wait to acquire the lock up to the timeout expiry; then fail. See the Javadoc.
  • 2. Re: Question on wait(timeout)?
    baftos Expert
    Currently Being Moderated
    906738 wrote:
    hi,

    The wait(timeout) method will try to reacquire the lock once the timeout has expired, up to the timeout it will release the lock. I hope it is correct.

    If it is so, what will happen if the another thread has holds the lock before the timeout expires, and keeps the lock after the timeout expires also,

    then what state the wait timeout thread will go? Will it keeps on waiting for lock like other threads?

    Thanks in advance
    From the Javadoc:
    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.
    My understanding is that the bullets establish the condition for wait() to return, while the last line above tells us when the code following the wait() will start executing. It may not be right away.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points