0% found this document useful (0 votes)
53 views

Inter-Thread Communication in Java Inter-Thread Communication or Co-Operation Is All About Allowing

Inter-thread communication in Java allows synchronized threads to communicate using wait(), notify(), and notifyAll() methods. These methods cause threads to pause execution and release locks so other threads may enter critical sections. Wait() pauses a thread until another calls notify() or notifyAll(), while notify() wakes one waiting thread and notifyAll() wakes all waiting threads. Together these methods provide synchronization between threads sharing the same objects and monitors.

Uploaded by

Gagan Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Inter-Thread Communication in Java Inter-Thread Communication or Co-Operation Is All About Allowing

Inter-thread communication in Java allows synchronized threads to communicate using wait(), notify(), and notifyAll() methods. These methods cause threads to pause execution and release locks so other threads may enter critical sections. Wait() pauses a thread until another calls notify() or notifyAll(), while notify() wakes one waiting thread and notifyAll() wakes all waiting threads. Together these methods provide synchronization between threads sharing the same objects and monitors.

Uploaded by

Gagan Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Inter-thread communication in Java

Inter-thread communication or Co-operation is all about allowing


synchronized threads to communicate with each other. Cooperation (Inter-
thread communication) is a mechanism in which a thread is paused running
in its critical section and another thread is allowed to enter (or lock) in the
same critical section to be executed. It is implemented by following methods
of Object class:
 wait()
 notify()
 notifyAll()

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()

Understanding the process of inter-thread communication


The point to point explanation of the above diagram is as follows:
1. Threads enter to acquire lock.
2. Lock is acquired by on thread.
3. Now thread goes to waiting state if you call wait() method on the
object. Otherwise it releases the lock and exits.
4. If you call notify() or notifyAll() method, thread moves to the notified
state (runnable state).
5. Now thread is available to acquire lock.
6. After completion of the task, thread releases the lock and exits the
monitor state of the object.

Why wait(), notify() and notifyAll() methods are defined in Object


class not Thread class?
It is because they are related to lock and object has a lock.

Difference between wait and sleep?


Let's see the important differences between wait and sleep methods.
wait() sleep()
wait() method releases the lock sleep() method doesn't release the
lock.
is the method of Object class is the method of Thread class
is the non-static method is the static method
is the non-static method is the static method
should be notified by notify() or after the specified amount of time,
notifyAll() methods sleep is completed.
 The wait() tells the calling thread to give up the monitor and go to
sleep until some other thread enters the same monitor and calls the
notify() or notifyAll()
 The notify() wakes up a thread that called the wait() on the same
object
 The notifyAll() wakes up all the threads that called the wait() on the
same object. One of the threads will be granted access

You might also like