---
title: "What are synchronized methods and blocks in Java?"  
description: "Synchronized methods and synchronized blocks are mechanisms used to ensure that only one thread can access a particular resource or section of code at"  
author: "Ashutosh Patel"  
published: 2024-07-23  
updated: 2024-07-23  
canonical: https://www.mindstick.com/blog/304524/what-are-synchronized-methods-and-blocks-in-java  
category: "java"  
tags: ["java", "synchronization", "programming language"]  
reading_time: 3 minutes  

---

# What are synchronized methods and blocks in Java?

This is important to prevent [concurrent access](https://www.mindstick.com/forum/160180/explain-the-role-of-rust-s-borrow-checker-in-ensuring-safe-concurrent-access-to-data) issues such as [data corruption](https://answers.mindstick.com/qa/99813/how-to-fix-issues-with-file-or-data-corruption-in-computer-systems) or instability in multithreaded environments.

#### Synchronized Methods

Synchronized method Method of reporting the `synchronized` keyword. When a thread calls a synchronized method, it automatically acquires an internal lock (or monitor lock) for the object in that method. This prevents other threads from calling any synchronized methods at the same time in the same object view.

## Example of a synchronized method

```java
public class Counter {
   private int count = 0;
   public synchronized void increment() {
       count++;
   }
   public synchronized int getCount() {
       return count;
   }
}
```

This example combines the `increment()` and `getCount()` methods. This ensures that only one thread can execute `increment()` or `getCount()`on the `Counter` instance at any given time. When one thread executes an i `ncrement()`, the other thread must wait until the first thread finishes [processing](https://www.mindstick.com/blog/254/background-processing-in-android) the method.

#### \
Synchronized Blocks

Synchronized blocks are used to provide synchronized access to a specific block of code instead of an entire method. This allows for better [synchronization](https://www.mindstick.com/articles/11983/synchronization-in-c-sharp) control.

**Syntax of [synchronized block](https://www.mindstick.com/forum/33649/why-does-it-seem-to-take-a-long-time-for-this-synchronized-block-to-get-a-lock)**

```java
synchronized (object) {
   // synchronized code block
}
```

\
**Object**

Refers to the object in which the horn was found. Any single thread can access the lock of a given object.

## Example

```java
public class SharedResource {
   private final Object lock = new Object();
   private int sharedVariable = 0;
   public void modifySharedVariable() {
       synchronized (lock) {
           sharedVariable++;
       }
   }
   public int getSharedVariable() {
       synchronized (lock) {
           return sharedVariable;
       }
   }
}
```

## In the above example

The `modifySharedVariable()` and `getSharedVariable()` methods use the lock object as a monitor to synchronize access to the sharedVariable. This ensures that if one thread executes modifySharedVariable(), the other thread cannot execute modifySharedVariable() or getSharedVariable() unless the lock is released.

#### Key Differences

## Scope of Synchronization

- **Synchronized methods-** Synchronize the entire body of the method. If all the work in a method needs to be done, it is easier.
- \ **Synchronized blocks-** Synchronize a specific block of code, allowing better control over parts of the method that need synchronization.

**Lock [Acquisition](https://www.mindstick.com/news/2246/following-elon-musk-s-acquisition-gm-suspends-its-twitter-advertising)**

\
**Synchronized methods-** Use this implicitly as an object launcher (on object instance calls to the method).

\
**Synchronized blocks-** Explicitly identify the object on which the lock can be obtained to enable synchronization changes.

## Performance Considerations

**Synchronized blocks-** Allow more granular control and can be more efficient than synchronizing entire methods, especially in situations where all method code does not need to be synchronized

#### When to Use

\
**Synchronized methods-** Used when all methods need to be planned and ease of lockout is desired.

\
**Synchronized blocks-** Used when synchronization is required for a particular block of code or when multiple locks are to be obtained on different parts of the method.

\
Synchronized methods and blocks are key tools for ensuring [thread safety](https://www.mindstick.com/forum/159021/what-are-rust-s-mechanisms-for-ensuring-thread-safety-in-concurrent-programming) and [preventing](https://www.mindstick.com/news/2244/issue-preventing-users-from-accessing-facebook-s-social-networking-platforms-has-been-resolved) data corruption in concurrent Java programs. They allow [maintenance](https://www.mindstick.com/articles/333912/maintenance-made-simple-how-online-tools-enhance-property-management) to shared resources and ensure that only one thread can switch resources at a time, thus maintaining consistency and correctness in multithreaded environments

**Also, Read:** [Discuss the volatile keyword in Java and its role in concurrency.](https://www.mindstick.com/blog/304522/discuss-the-volatile-keyword-in-java-and-its-role-in-concurrency)

---

Original Source: https://www.mindstick.com/blog/304524/what-are-synchronized-methods-and-blocks-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
