Difference between sleep(), wait(), and yield()? — Cracked Java
// Concurrency & Multithreading · Thread Fundamentals & Lifecycle
MidTheoryTrickEPAMAmazon

Difference between sleep(), wait(), and yield()?

All three pause the current thread, but they differ in what they touch: sleep() and yield() are static Thread methods that never release any lock; wait() is an Object method that releases the monitor and parks the thread until notified. The lock-release distinction is the heart of the question.

Thread.sleep(ms)

Static; pauses the current thread for a duration (state TIMED_WAITING), then it returns to RUNNABLE on its own. Crucially it does not release any locks the thread holds — sleeping inside synchronized keeps everyone else out. Throws InterruptedException. Use it for delays/backoff, never for inter-thread coordination.

Object.wait()

Instance method on the monitor object; must be called while holding that object's lock (else IllegalMonitorStateException). It atomically releases the lock and parks the thread (WAITING, or TIMED_WAITING for wait(ms)) until another thread calls notify()/notifyAll() on the same object. On wakeup it re-acquires the lock before returning. It exists precisely for the guarded-block / producer-consumer protocol.

Thread.yield()

Static hint to the scheduler that the current thread is willing to give up the CPU to peers of equal priority. It releases no locks, may be ignored entirely (it's advisory and platform-dependent), and the thread stays RUNNABLE. Rarely useful outside spin-wait tuning.

synchronized (queue) {
    while (queue.isEmpty()) {
        queue.wait();        // releases 'queue' lock, sleeps, re-acquires on notify
    }
    return queue.poll();
}

Thread.sleep(500);           // keeps every held lock; just a timed pause
Thread.yield();              // "I'll step aside" hint; no guarantee

Quick contrast

OperationBestAverageWorstNote
sleep(ms)Thread (static)NoTIMED_WAITINGTimes out by itself; throws InterruptedException
wait()Object (instance)Yes — releases monitorWAITING / TIMED_WAITINGMust hold the lock; woken by notify/notifyAll
yield()Thread (static)NoRUNNABLEAdvisory scheduler hint; may be ignored

(Columns above: method, declaring class, releases lock?, resulting state, note.)

Mark your status