Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Java FAQ: Top Questions

27. What is the difference between sleep() and wait() in Java?

sleep() and wait() are methods used in Java multithreading, but they differ in purpose and behavior.

  • sleep():
    • Static method of Thread class.
    • Pauses the current thread for a specified time without releasing locks.
    • Used for delaying execution.
  • wait():
    • Instance method of Object class.
    • Releases the monitor (lock) and waits for notify() or notifyAll() .
    • Used for inter-thread communication within synchronized blocks.