Difference between Wait and Sleep in Java
In Java, wait and sleep are the concept of multithreading. Wait and Sleep are the methods used to pause a process for few seconds and go a thread in the waiting state, respectively. Let's understand both of them one by one to get more information about them.
The Sleep () method is related to the Thread class that is used to stop the execution of the current Thread for few seconds. The Sleep () method takes the sleeping time in milliseconds. The monitor's ownership is not lost when we use the Sleep () method and start the execution again from where it stops. In simple words, the Sleep() method is responsible for sending the current Thread into the "Non Runnable" state.
The Wait() method is related to the Object class. The Wait() method is responsible for sending the calling thread into the waiting state. The Thread remains in the waiting state until another thread doesn't invoke the notify() or notifyAll() method for that object. The Thread resumes the execution after obtaining the ownership of the monitor.
Before understanding the differences between both of them, let's understand the similarities between them. So, both the Wait() and Sleep() methods are the native methods that make the current Thread go into the Non-Runnable State.
Let's take an example to understand the similarities of both of them.
Difference b/w Wait() and Sleep()
Let's understand the difference between Wait () and Sleep() method:
Next TopicExecutor Framework Java