---
title: "What is the difference between wait() and sleep() in Java?"  
description: "What is the difference between wait() and sleep() in Java?"  
author: "Ashutosh Patel"  
published: 2024-07-19  
updated: 2024-07-19  
canonical: https://www.mindstick.com/interview/33961/what-is-the-difference-between-wait-and-sleep-in-java  
category: "java"  
tags: ["java", "methods", "java method"]  
reading_time: 2 minutes  

---

# What is the difference between wait() and sleep() in Java?

#### Difference between wait() and sleep()

Both `wait()` and `sleep()` methods are used in Java for concurrent scheduling, but serve different purposes:

## wait()

- In synchronization contexts, it is used to suspend the execution of a thread until another thread shows up.
- The call must come from a synchronized block or method.
- Release the lock of the object being called.

## Example-

```java
synchronized (sharedObject) {
   while (!condition) {
       sharedObject.wait(); // Thread waits until condition is met
   }
   // Continue execution after condition is true
}
```

## sleep()

- Used to suspend the use of a thread for a specified period of time.
- Nothing can release the lock.
- It is not tied to synchronization and can be invoked from any context.

## Example-

```java
try {
   Thread.sleep(1000); // Pauses execution for 1 second
} catch (InterruptedException e) {
   e.printStackTrace();
}
```

Specifically, `wait()` is used for communication between threads in synchronous context, while `sleep()` is used to pause execution for a specified period of time, regardless of session

**Also, Read:** [What are the key features introduced in Java 8?](https://www.mindstick.com/forum/160961/what-are-the-key-features-introduced-in-java-8)

## Answers

### Answer by Ashutosh Patel

#### Difference between wait() and sleep()

Both `wait()` and `sleep()` methods are used in Java for concurrent scheduling, but serve different purposes:

## wait()

- In synchronization contexts, it is used to suspend the execution of a thread until another thread shows up.
- The call must come from a synchronized block or method.
- Release the lock of the object being called.

## Example-

```java
synchronized (sharedObject) {
   while (!condition) {
       sharedObject.wait(); // Thread waits until condition is met
   }
   // Continue execution after condition is true
}
```

## sleep()

- Used to suspend the use of a thread for a specified period of time.
- Nothing can release the lock.
- It is not tied to synchronization and can be invoked from any context.

## Example-

```java
try {
   Thread.sleep(1000); // Pauses execution for 1 second
} catch (InterruptedException e) {
   e.printStackTrace();
}
```

Specifically, `wait()` is used for communication between threads in synchronous context, while `sleep()` is used to pause execution for a specified period of time, regardless of session

**Also, Read:** [What are the key features introduced in Java 8?](https://www.mindstick.com/forum/160961/what-are-the-key-features-introduced-in-java-8)


---

Original Source: https://www.mindstick.com/interview/33961/what-is-the-difference-between-wait-and-sleep-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
