Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which one of the following statements about sleeping while holding a lock is true?

  • A.Calling sleep() while holding a lock blocks other threads that need that lock for the whole duration.
  • B.Calling sleep() briefly releases the lock so other threads can make progress, then reacquires it on waking.
  • C.Calling sleep() while holding a lock throws IllegalMonitorStateException.
  • D.Calling sleep() while holding a lock promotes the lock to a read-only lock.

Answer: A

Because sleep does not give up ownership of any monitor, a sleeping thread that holds a lock keeps it. Any thread that needs the same lock stays blocked until the sleeper wakes and leaves the synchronized region. This is the difference from wait(), which does release the monitor it is called on.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.