Sitemap

Member-only story

What Really Happens Inside a synchronized Block in Java?

What happens inside a synchronized block that makes it thread-safe?

--

If you’re learning Java and working with threads, you’ve probably seen the synchronized keyword used like this:

synchronized (someObject) {
// critical section
}

But what’s actually going on behind the scenes?

How does Java make sure only one thread can run this block at a time?
What happens inside a
synchronized block that makes it thread-safe?

In this article, we’ll break it down step-by-step — no complex theory, just clear, practical insight. Let’s unlock what synchronized is really doing under the hood.

First, What Is a synchronized Block?

In Java, synchronized is used to protect code from being accessed by multiple threads at the same time, especially when working with shared data.

Example:

public void updateBalance() {
synchronized (this) {
// only one thread can run this at a time for this object
balance += 100;
}
}

The block of code inside synchronized is called a critical section — it’s where only one thread is allowed in at a time.

What Happens Inside a synchronized Block?

--

--

No responses yet