LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Find the Error · Question 5

Error: using lock() as a condition Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links