Question 5
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
import java.util.concurrent.locks.ReentrantLock;
public class Main {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
if (lock.lock()) {
try { System.out.println("in"); } finally { lock.unlock(); }
}
}
}
- A unlock must come first
- B lock() returns void (it waits until the lock is acquired); use tryLock(), which returns a boolean, if you need a condition Correct
- C ReentrantLock needs a fairness argument
- D try-finally is not allowed with locks
Correct Answer: Option B
javac: "incompatible types: void cannot be converted to boolean".
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!