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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / alan

Thats a very nice answer Saurabh.
makes my understanding clear about both sleep() and wait().

Which one is the best site to look for more java related
questions?

Is This Answer Correct ?    0 Yes 0 No

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

Answer / amit singh

every class has an object lock but when you used
synchronized block or synchronized method
means to say the when you use synchronized keyword then the
object lock come in an existence without you don't get the
lock on particular object you
must know that wait()
1) what is the purpose of when you call wait on the object
which lock you get when you use synchronized block or method
then particular thread immediate leave the
lock so means to say that wait must to use when you get the
lock on the particular object without

2)its your turns to think that there is any mendotary
to call Thread.sleep() method in synchronized block
so whe there is no relation b/t sleep and Monitor(i'm saying
lock on the object)
why youthink it like that why the sleep() doesn't leave the
lock and wait() leave the lock

3) ther is no realtion between object lock and sleep
amit singh
amitsing2008@gmail.com

Is This Answer Correct ?    0 Yes 2 No

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

Answer / balamurugan

Wait Method have parameters as MilliSecond after that
particular time over thread start execution.So Wait Method
Leave the Lock.

But in Sleep() have no any parameter so the thread have no
clue to leave the lock . that's y sleep method does not
leave the lock the programmer need to explicitly write
another method like notify() to execute.

Is This Answer Correct ?    0 Yes 4 No

Post New Answer

More Java Related AllOther Interview Questions

What is proxy object in java?

0 Answers  


What do you understand by a variable?

0 Answers  


What's the difference between local, global and universal groups? : java security

0 Answers  


What is an actionevent in java?

0 Answers  


Where is singleton design pattern used in java?

0 Answers  


Which type of stream is in java?

0 Answers  


Topic- looping,function overloading,nesting ,polymorphism. Aim - to write a function with a name buzz-buds,that will check whether the given numbers are buddies or not on the basis of no. of parameters passed during function calling.

0 Answers  


What is the content of the Java 2 security policy file?

1 Answers  


What is data persistence in java?

0 Answers  


Which instutute is offering course for rhino jain slee

0 Answers  


Are jvm’s platform independent?

0 Answers  


What is xml file in java?

0 Answers  


Categories