Question 425 of 509
Controlling Program FlowhardMultiple ChoiceObjective-mapped

Quick Answer

The correct choice is to use a Lock with tryLock and a timeout, releasing all locks if the timeout expires. This approach directly breaks the circular wait condition—one of the four necessary conditions for deadlock—by allowing a thread to back off and release any locks it already holds when it cannot acquire all required locks within a specified time window. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this scenario tests your understanding of deadlock prevention strategies in concurrent programming, often appearing in questions about fine-grained locking versus coarse-grained alternatives. A common trap is choosing consistent global ordering, which prevents deadlock but requires restructuring all lock acquisitions and can reduce throughput in dynamic key sets. Remember the memory tip: “tryLock and release—break the circle with ease.”

1Z0-829 Controlling Program Flow Practice Question

This 1Z0-829 practice question tests your understanding of controlling program flow. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

In a large enterprise application, a concurrent caching system is implemented using a ConcurrentHashMap that is accessed by multiple threads concurrently. The cache performs atomic operations on individual keys, but some operations require updates on multiple keys. To ensure consistency, the code acquires intrinsic locks on the keys using synchronized blocks. Over time, the system has been experiencing intermittent deadlocks. During post-mortem analysis, it was found that thread A holds a lock on key X and is waiting for key Y, while thread B holds a lock on key Y and is waiting for key X. The development team needs to redesign the locking strategy to eliminate these deadlocks while maintaining high throughput and minimizing code changes. They consider the following proposals: replacing ConcurrentHashMap with Collections.synchronizedMap, using a single ReentrantLock for all cache operations, always acquiring locks on keys in a consistent global order, or using a Lock with tryLock and a timeout and releasing all locks if timeout expires. Based on best practices in concurrent programming and considering the requirements to avoid deadlocks and maintain performance, which approach should they choose?

Clue words in this question

Noticing these words before you look at the options changes how you read each choice.

  • Clue: "best"

    Why it matters: Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.

  • Clue: "always"

    Why it matters: Absolute qualifier. An answer using 'always' is only correct if there are genuinely no exceptions — absolute statements are often wrong in networking.

Question 1hardmultiple choice
Read the full NAT/PAT explanation →

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 a Lock with tryLock and a timeout, and release all locks if timeout expires.

Option C is correct because using tryLock with a timeout allows threads to back off and release all acquired locks if they cannot obtain all required locks within a specified time, which breaks the circular wait condition that causes deadlocks. This approach maintains high throughput by avoiding coarse-grained locking and minimizes code changes by only modifying the lock acquisition logic, not the underlying ConcurrentHashMap structure.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

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 single ReentrantLock for all cache operations.

    Why it's wrong here

    A single lock reduces concurrency to essentially single-threaded access, which damages throughput and can cause contention.

  • Replace ConcurrentHashMap with Collections.synchronizedMap.

    Why it's wrong here

    This would synchronize all map methods, reducing concurrency significantly and potentially causing contention, but deadlocks could still occur if nested synchronized blocks are used.

  • Use a Lock with tryLock and a timeout, and release all locks if timeout expires.

    Why this is correct

    This approach uses tryLock with a timeout to avoid indefinite waiting; if a thread cannot acquire all locks within the timeout, it releases acquired locks and retries, effectively preventing deadlock while maintaining concurrency.

    Clue confirmation

    The clue words "best", "always" in the question point toward this answer.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Always acquire locks on keys in a consistent global order.

    Why it's wrong here

    While this prevents deadlock in theory, it requires strict enforcement across all code paths and can be error-prone and difficult to maintain.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates often choose consistent global ordering (Option D) as the textbook deadlock prevention technique, but the question emphasizes 'minimizing code changes' and 'maintaining high throughput,' making the tryLock approach more practical for an existing ConcurrentHashMap-based system with dynamic keys.

Detailed technical explanation

How to think about this question

The tryLock(timeout, TimeUnit) method from java.util.concurrent.locks.Lock interface attempts to acquire the lock within the given waiting time and returns false if the lock is not available, allowing the thread to release any already-held locks and retry. This technique, often combined with a randomized backoff, resolves deadlocks by ensuring that threads do not wait indefinitely for each other, effectively breaking the circular wait condition without requiring a global lock order. In real-world systems like database transaction managers, this pattern is used to handle lock contention gracefully.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A small business has 20 workstations on the 192.168.1.0/24 network and one public IP from its ISP. The router uses PAT (NAT overload) so all 20 devices share one public address using different source ports. NAT questions test whether you understand the four address terms and which direction each translation applies.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related 1Z0-829 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free 1Z0-829 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this 1Z0-829 question test?

Controlling Program Flow — This question tests Controlling Program Flow — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Use a Lock with tryLock and a timeout, and release all locks if timeout expires. — Option C is correct because using tryLock with a timeout allows threads to back off and release all acquired locks if they cannot obtain all required locks within a specified time, which breaks the circular wait condition that causes deadlocks. This approach maintains high throughput by avoiding coarse-grained locking and minimizes code changes by only modifying the lock acquisition logic, not the underlying ConcurrentHashMap structure.

What should I do if I get this 1Z0-829 question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Are there clue words in this question I should notice?

Yes — watch for: "best", "always". Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

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 →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 25, 2026

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.

Loading comments…

Sign in to join the discussion.

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.