what is the difference between wait and sleep method in java?
what is the difference between wait and sleep method in java?
sleep():
It is an static method in thread class it make current thread into "Not Runnable"state for specific amount of time.
wait():
It is a method on object class make the current thread into "Not Runnable"state it is called on object not thread.
The fundamental difference is wait() is from Object and sleep() is static method of Thread . The major difference is that wait() releases the lock while sleep() doesn't releas any lock while waiting. The wait() is used for inter-thread communication while sleep() is used to introduce pause on execution, generally.
major difference is that wait() releases the lock while sleep() doesn't releas any lock while waiting.
In sleep() methods used to hold the process for few seconds
In wait() methods thread go to waiting state not come back automatically until call (notify,notify all) methods
Sleep method just send your program to go somewhere to do another task
Those basic distinction is wait() will be starting with object Furthermore sleep() is static system for string. The significant distinction will be that wait() discharges the lock same time sleep() doesn't releas whatever lock same time Holding up. The wait() may be utilized for inter-thread correspondence same time sleep() may be used to present stop around execution, by and large.
Truck Dispatch Software | Taxi App Development | Hire Angularjs Developers | Mobile Application Development Company | Top Mobile App Developers | iPhone App Development Company | Find Xamarin Certified Developers| peer-to-peer ridesharing| how to create a ride share app| app development companies usa | Hire Kotlin Developers
One key difference not yet mentioned is that while sleeping a Thread does not release the locks it holds, while waiting releases the lock on the object that wait() is called on.
synchronized(LOCK) {
Thread.sleep(1000); // LOCK is held
}
synchronized(LOCK) {
LOCK.wait(); // LOCK is not held
}
The fundamental difference is wait() is from Object and sleep() is static method of Thread . The major difference is that wait() releases the lock while sleep() doesn't releas any lock while waiting. The wait() is used for inter-thread communication while sleep() is used to introduce pause on execution.
You can get more info on Stack Overflow.
Sleep () is a static method of thread class that is responsible for making the current thread in not runnable state for a particular time frame. At the same time, wait () is the method of an object class that will make the current object in not a runnable state. You can use any of them as per the nature of a program.
Bookmarks