Java Threading · interview question
In what scenario might a ReentrantLock be preferred over a synchronized block?
Multiple choiceMediumchapter-15-curatedpremiumjava-threadingrace-conditions
Answer and explanation
Correct answer
D. When you need to try locking without blocking.
Why
ReentrantLock offers more flexibility, such as trying to acquire a lock without blocking, or waiting with a timeout, which can be crucial in complex concurrency scenarios.
Test yourself on Java Threading
A free test with instant, explained results. No account needed.
More Java Threading questions
- Which method would you use to create a Virtual Thread in Java?
- Which ExecutorService method can submit a Runnable for execution?
- What happens if a task throws an unchecked exception in an ExecutorService?
- Which method in ExecutorService can be used to attempt a graceful shutdown, allowing current tasks to complete before…
- In a high-concurrency e-commerce flash sale, which construct best avoids a race condition when updating inventory…
- Why do teams use an ExecutorService instead of manually creating and starting threads in production code?