Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

why the sleep method does not leave the lock and wait method
leave the lock

Answer Posted / saurabh m. chande

for wait(), we ask that thread to wait for defined period of time. So, for that defined period of time, CPU just cant sit idle instead it prefers to run another thread which is looking for acquiring a lock by CPU. If wait() don't release the lock, considering the synchronization in mind, it will not serve any purpose but CPU will wait for the defined period being just idle. Then, there is no point of implementing Threads in our application.
sleep() : User is damn sure that he wants to run the thread in sleep mode. So if he uses wait() instead of sleep, then the thread -
1. will wait for defined period of time.
2. when the time is over, thread will go into runnable state and not in running state.
3. not sure when CPU will pick up this thread for further execution.
To avoid all the 3 reasons, sleep will not leave the lock on the thread. Instead it prefers to keep the lock on the thread even if it is in sleep mode. But it is sure that, once the sleep mode is over, it is having a lock and can directly put the thread in running state without further waiting.

- Saurabh M. Chande

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why dependency injection is used in java?

1042


What is type inference? Is type inference available in older versions like java 7 and before 7 or it is available only in java se 8?

916


What is the purpose of jdk?

926


Can we write lambda without functional interface?

911


How can one prove that the array is not null but empty?

934


Can you compile java into exe?

1042


What is the difference between lambda expression and anonymous methods?

932


How do I install java on windows?

917


What is a cookie in java?

966


What is cloning in java?

929


How can you work with permissions from your .net application? : java security

911


What is a controller in java?

1039


Why doesn’t the main method throw an error with no arguments?

937


Why oracle type 4 driver is named as oracle thin driver?

857


What is transaction management in java?

916