DEV Community

Pramod Bablad
Pramod Bablad

Posted on

wait() Vs sleep() Methods In Java

wait() :

1) The thread which calls wait() method releases the lock it holds.
2) The thread regains the lock after other threads call either notify() or notifyAll() methods on the same lock.
3) wait() method must be called within the synchronized block.
4) wait() method is a member of java.lang.Object class.
5) wait() method is always called on objects.
6) wait() is a non-static method of Object class.
7) Waiting threads can be woken up by other threads by calling notify() or notifyAll() methods.
8) To call wait() method, thread must have object lock.

sleep() :

1) To call wait() method, thread must have object lock.
2) No question of regaining the lock as thread doesn’t release the lock.
3) sleep() method can be called within or outside the synchronized block.
4) sleep() method is a member of java.lang.Thread class.
5) sleep() method is always called on threads.
6) sleep() is a static method of Thread class.
7) Sleeping threads can not be woken up by other threads. If done so, thread will throw InterruptedException.
8) To call sleep() method, thread need not to have object lock.

See more at https://javaconceptoftheday.com/difference-between-wait-and-sleep-methods-in-java/

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay