What is the difference between sleep and wait in Java?

  1. sleep is a static method of the Thread class, while wait is an instance method of the Object class.
  2. The sleep method pauses the current thread for a period of time without releasing the lock, while the wait method pause the current thread and release the lock.
  3. The sleep method must be used within a synchronized block or method, while the wait method must be used within a synchronized block.
  4. The sleep method does not have to be woken up, it will automatically resume execution when the time is up, whereas the wait method needs to be woken up by notify or notifyAll.
  5. The sleep method can be used anywhere, while the wait method can only be used inside a synchronized block.
Leave a Reply 0

Your email address will not be published. Required fields are marked *