How to Use Java synchronized to Avoid Dirty Reads and Thread Interference
This article explains Java's synchronized keyword, demonstrating how synchronized methods and blocks control thread execution order, prevent dirty reads, and illustrate the differences between synchronized and non‑synchronized code through multiple runnable examples.
synchronized Method
The first example shows two threads calling methodA() on the same MyObject instance without synchronization, resulting in overlapping execution timestamps. By declaring methodA as synchronized, the threads acquire the object lock and execute sequentially, as shown by the printed timestamps.
Mixed synchronized and non‑synchronized Methods
When methodA is synchronized and methodB is not, thread A holds the lock for methodA while thread B can still invoke methodB asynchronously. Making both methods synchronized forces them to run one after the other.
Dirty Read Example
A class PublicVar has synchronized setValue but non‑synchronized getValue. A thread sets new values while the main thread calls getValue before the setter finishes, producing a dirty read (partial update). Synchronizing both methods eliminates the dirty read because the operations are executed in order.
Synchronization Inheritance
A subclass Sub overrides a synchronized method from Main. Calls to serviceMethod on a Sub instance show that the subclass method runs asynchronously, while the superclass synchronized method still enforces exclusive access, demonstrating that synchronization is not inherited automatically.
synchronized Code Block
Long‑running tasks placed inside a synchronized block ( synchronized(this){ ... }) allow other threads to execute non‑synchronized code concurrently, reducing overall execution time compared to synchronizing the entire method.
Block‑Level Synchronization Across Methods
Multiple methods using synchronized(this) share the same monitor; when one thread enters any synchronized block, other threads are blocked from entering any other synchronized block on the same object. The example with serviceMethodA and serviceMethodB confirms this behavior.
Locking the Current Object
Both synchronized methods and synchronized(this) blocks lock the current object. Demonstrations with a Task class show that while a synchronized block protects a critical section, other non‑synchronized methods can still run concurrently unless they are also declared synchronized.
Overall, the article provides practical Java code samples and console output to illustrate how the synchronized keyword and synchronized blocks control thread interaction, prevent race conditions, and avoid dirty reads.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
