What does wait method do ?

What does wait method do ?

The wait() method is defined in the Object class. The method should only be called by a thread that has ownership of the object\'s monitor, which usually means it is in a synchronized method or statement block. 

Threads - wait() method do :

    * the wait() method causes a thread to release the lock it is holding on an object; allowing another thread to run.
    * the wait() method is defined in the Object class.
    * wait() can only be invoked from within synchronized code.
    * it should always be wrapped in a try block as it throws IOExceptions.

There are actually three wait() methods

   1. wait()
   2. wait(long timeout)
   3. wait(long timeout, int nanos) 

 The timeout is measured in milliseconds. when wait() is called, the thread becomes disabled for scheduling and lies dormant until one of four things occur:

   1. another thread invokes the notify() method for this object and the scheduler arbitrarily chooses to run the thread
   2. another thread invokes the notifyAll() method for this object
   3. another thread interrupts this thread
   4. the specified wait() time elapses 

By:Jalees
Date:2015-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher