Course contentsShow
← Java
Lesson 2658 of 2,87049. Synchronization and LockingFree lesson

Mixing synchronized and Non-synchronized Methods

Non-synchronized methods run independently; only synchronized methods coordinate via the lock, creating potential issues.

Mixing synchronized and Non-synchronized Methods

What you'll learn: You'll understand why combining synchronized and non-synchronized methods in the same class creates dangerous inconsistencies and race conditions.


When a class contains both synchronized and non-synchronized methods that access shared mutable state, you create a synchronization hole that undermines thread safety completely.

The Core Problem

Synchronized methods coordinate access by acquiring the object's lock—only one thread can execute any synchronized method at a time. Non-synchronized methods ignore the lock entirely. They run independently, without waiting for or respecting the lock that synchronized methods use.

This means while one thread safely executes a synchronized method (holding the lock), another thread can simultaneously execute a non-synchronized method on the same object—accessing the same fields without any coordination.

A Concrete Example

public class BrokenCounter {
    private int count = 0;

    public synchronized void increment() {
        count++;  // Protected by lock
    }

    public int getCount() {  // NOT synchronized
        return count;  // Reads count WITHOUT lock
    }
}

When increment() runs, it holds the lock. But getCount() doesn't acquire the lock—it can read count while another thread is mid-increment, potentially seeing a partially updated or stale value.

Why This Breaks Everything

Think of synchronized methods as requiring a "permission ticket" to enter a restricted room (the object's state). Non-synchronized methods are side doors that bypass security entirely. Your "restricted room" isn't actually restricted if anyone can walk through the side doors.

For thread safety, all methods that read or write shared mutable state must be synchronized on the same lock.

Key Takeaway:
Mixing synchronized and non-synchronized methods that access the same shared state destroys thread safety—non-synchronized methods bypass the lock entirely, creating race conditions.