Difficulty: Easy
Correct Answer: It print 12 12 12 12
Explanation:
Introduction / Context:
This code launches two threads sharing a single Runnable instance. Inside run(), assignments to x and y are protected by synchronized(this). The question asks whether the final prints are deterministic and what values will appear.
Given Data / Assumptions:
Concept / Approach:
Because all writes to x and y happen under the same monitor and always set the same constants (12 and 12), the values cannot diverge. Although the final println occurs outside synchronized, the JMM still guarantees that some assignment has occurred before printing since each thread itself performs the assignments, then prints. There is no deadlock: both synchronized blocks are short and use a single, consistent monitor (this).
Step-by-Step Solution:
Verification / Alternative check:
Even without synchronization, constant assignments would still lead to 12 12, but synchronization ensures no torn writes or reordering issues for visibility across threads.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming prints must be inside synchronized to see consistent values; here each thread prints its own last writes.
Final Answer:
It print 12 12 12 12
Discussion & Comments