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

Discussion

Which of these statements about releasing a ReentrantLock is correct?

  • A.A ReentrantLock is released automatically when the enclosing method returns or throws.
  • B.A ReentrantLock is released automatically when the thread is interrupted.
  • C.A ReentrantLock cannot be released by the thread that acquired it.
  • D.A ReentrantLock should be released in a finally block.

Answer: D

Unlike synchronized, whose monitor is released whichever way the block exits, an explicit lock is released only when you call unlock. Skipping the finally means one thrown exception leaves the lock held for ever. What you gain for that care is tryLock, timed waits and interruptible acquisition.

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

Post your comment

Please log in or register to post a comment.