Inter Thread Communication:
Before you start reading this post please read this post for thread creation concept,read this post for actual thread class and read this life cycle of a thread. These links will give you a better idea how a thread works. Coming back to the topic,Inter thread communication happens in three ways.
- Through common shared data
- Using thread control methods
- Through inter process communication
All the threads in the same program share the same memory space.If an object is accessible to various thread then these threads share access to that object’s data members and thus communicate with each other.
There are three ways by which threads communicate to each other by this technique.
They are as follows:
- suspend();–> A thread can suspend itself and waits for another thread to resume it.
- resume();–>A thread can wake another thread (waiting state) through resume method. Then they can run concurrently.
- join()–>This method is used for caller thread to wait for completion of the called thread.
There are three methods available for inter process communication in this technique. They are as follows:
- wait();–>This method tells the caller thread to give up the monitor and make the calling thread wait until either a time out occurs or another thread calls the same thread’s notify() or notifyAll() method.
- notify();–>This method wakes up the only one(first) waiting thread called wait() or the same object
- notifyAll();–>This method will wake up all the threads that have been called by wait() on the same object.