Java SE 17 Developer — Question 17
Which statement is true?
Answer options
- A. The lock() method returns a boolean indicator immediately regardless if it has or has not managed to acquire the lock.
- B. The lock() method returns a boolean indicator immediately if it has managed to acquire the lock, otherwise it waits for the lock acquisition.
- C. The tryLock() method returns a boolean indicator immediately if it has managed to acquire the lock, otherwise it waits for the lock acquisition.
- D. The tryLock() method returns a boolean indicator immediately regardless if it has or has not managed to acquire the lock.
Correct answer: D
Explanation
The correct answer is D because the tryLock() method is designed to return a boolean value instantly to indicate whether the lock has been acquired or not, without blocking. Options A and B refer to the lock() method, which does not work in the same way, as it can block until the lock is available. Option C incorrectly describes the behavior of tryLock() by suggesting it can wait for lock acquisition, which it does not do.