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

Discussion

Which of these statements about Thread.sleep and the monitors a sleeping thread holds is correct?

  • A.sleep() pauses the thread whose Thread object it is invoked on, retaining every monitor.
  • B.sleep() pauses the calling thread and releases the monitors it holds.
  • C.sleep() pauses the calling thread and releases only the monitor of the object it is called on.
  • D.sleep() is static and pauses the thread that calls it, retaining every monitor it holds.

Answer: D

Thread.sleep is static, so it always affects the thread that calls it; writing t.sleep(100) still puts the current thread to sleep, not t. The javadoc states the thread does not lose ownership of any monitors, so a lock held across a sleep stays held and other threads waiting on it make no progress.

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

Post your comment

Please log in or register to post a comment.