
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Wait and Sleep in Java
Wait() - The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify() method or the notifyAll() method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
Sleep() - This method causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors. It sends the current thread into the “Not Runnable” state for a specified amount of time.
Sr. No. | Key | Wait | Sleep |
---|---|---|---|
1 | Class | Wait() method belongs to Object class | Sleep() method belongs to Thread class |
2 | Lock Release | Wait() releases the lock on an object | It does not release lock on an object |
3 | Calling context | Wait() can be called on object itself | Sleep() can be called on thread |
4. | Wake-up condition | until call notify(), notifyAll() from object | until at least time expire or call interrupt |
5 | spurious wakeups | Program can get spurious wakeups | It will not get spurious wakeups. |
Example of SynchronizedMap
synchronized(lockedObject){ while(condition == true){ lockedObject.wait() //releases lockedObject lock } Thread.sleep(100); //puts current thread on Sleep }
- Related Questions & Answers
- Difference Between sleep() and wait() Method in Java
- Differences between wait() and sleep() method in Java?
- Difference between Stop and Wait protocol and Sliding Window protocol
- Difference between Stop and Wait, GoBackN and Selective Repeat protocols
- Differences between wait() and join() methods in Java
- Explain difference between Stop and Wait protocol and Sliding Window protocol
- Difference between Java and JavaScript.
- Difference between Go and Java.
- Difference Between C++ and Java
- Sleep in JavaScript delay between actions?
- Is MySQL's SLEEP() function a busy-wait? How to implement it?
- Difference between Java and C language
- Difference between constructor and method in Java
- Difference between HashMap and HashTable in Java.
- Difference between StringBuilder and StringBuffer in Java
Advertisements