Java synchronization basics: identify the single true statement about synchronized methods/blocks and thread behavior.

Difficulty: Easy

Correct Answer: If a class has synchronized code, multiple threads can still access the nonsynchronized code.

Explanation:


Introduction / Context:
This question checks foundational knowledge of Java’s synchronized keyword applied to methods and blocks, and how it interacts with static members, instance members, and thread state changes such as sleep().


Given Data / Assumptions:

  • Synchronized is a modifier for methods and a block statement prefix; it is not a variable modifier.
  • sleep() is a Thread API that pauses execution but does not affect monitor ownership.
  • Static synchronized methods lock the Class object; instance synchronized methods lock the receiver instance.


Concept / Approach:
Confirm each option against the language rules. Only one statement should be fully accurate without extra caveats.


Step-by-Step Solution:
Option A: False. Static methods can be declared synchronized; they acquire the associated Class object’s monitor.Option B: True. Synchronization only guards the specific synchronized regions. Unprotected (non-synchronized) methods/blocks may run concurrently regardless of other synchronized parts.Option C: False. synchronized is not a field modifier; variables are not “marked synchronized.” Protection comes from synchronizing the code that accesses them.Option D: False. Thread.sleep(...) does not release any monitors held by the thread; locks are released only by exiting synchronized scopes (or by wait(), which releases the specific monitor while waiting).


Verification / Alternative check:
Create a class with both synchronized and unsynchronized methods; invoke them from multiple threads and observe that unsynchronized calls may interleave freely.


Why Other Options Are Wrong:
They misstate synchronized’s allowable targets (A/C) or the semantics of sleep() vs. monitor release (D).


Common Pitfalls:
Thinking synchronized is a property of data rather than code; assuming sleep() behaves like wait(); confusing static and instance locking.


Final Answer:
If a class has synchronized code, multiple threads can still access the nonsynchronized code.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion