Difference between Wait and Sleep in JavaIn 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. Sleep()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. Wait()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. WaitSleepSimilaritiesExample.java Output Difference b/w Wait() and Sleep()Let's understand the difference between Wait () and Sleep() method:
Next TopicExecutor Framework Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India