Question 325 of 513
1Z0-829 Java I/O API and Securing Applications Practice Question
A developer is building a file synchronization tool that runs on multiple threads. Multiple threads may read and write to the same file concurrently. The developer wants to ensure that a thread does not read a file while another thread is writing to it, and that concurrent reads are allowed. Which locking mechanism should be used?
⚠ Common exam trap
Test-takers frequently confuse in-memory concurrency mechanisms (like ReentrantReadWriteLock or synchronized) with file-level locking, forgetting that file access across threads or processes requires OS-level coordination provided by FileLock.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use java.nio.channels.FileLock with shared lock for reading and exclusive lock for writing.
Java.nio.channels.FileLock provides a platform-independent mechanism for locking files, supporting shared locks for concurrent reads and exclusive locks for writes. This ensures that a thread cannot read a file while another thread is writing to it, while allowing multiple threads to read simultaneously, which matches the requirement exactly.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use a ReentrantReadWriteLock in Java with the file as a resource.
Why it's wrong here
ReentrantReadWriteLock is for in-memory synchronization, not for file-level locking across threads.
- ✗
Use synchronized blocks on the File object.
Why it's wrong here
Synchronized blocks cannot coordinate access across JVMs or even across threads if the File object is not shared appropriately.
- ✗
Use a Semaphore with permits equal to the number of threads.
Why it's wrong here
Semaphore does not differentiate between read and write operations.
- ✓
Use java.nio.channels.FileLock with shared lock for reading and exclusive lock for writing.
Why this is correct
FileLock with shared/exclusive modes allows concurrent reads and exclusive writes, and works across processes.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 25, 2026
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.