Inter-Thread Communication in Java Inter-Thread Communication or Co-Operation Is All About Allowing
Inter-Thread Communication in Java Inter-Thread Communication or Co-Operation Is All About Allowing
1) wait() method
Causes current thread to release the lock and wait until either another
thread invokes the notify() method or the notifyAll() method for this object,
or a specified amount of time has elapsed.
The current thread must own this object's monitor, so it must be called from the synchronized method only otherwise
it will throw exception.
Method Description
public final void wait()throws
waits until object is notified.
InterruptedException
public final void wait(long timeout)throws waits for the specified
InterruptedException amount of time.
2) notify() method
Wakes up a single thread that is waiting on this object's monitor. If any
threads are waiting on this object, one of them is chosen to be awakened.
The choice is arbitrary and occurs at the discretion of the implementation.
Syntax:
public final void notify()
3) notifyAll() method
Wakes up all threads that are waiting on this object's monitor. Syntax:
public final void notifyAll()