Inter Thread Communication is one of the distinct facility in multi threading application development of java. In real world Inter Thread communication based applications of java works faster when compared to all the other applications in java.
Inter-thread communication is all about making synchronized threads communicate with each other. It is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter in the same critical section to be executed. It is implemented by following methods of
Object classwait() : Tells the calling thread to give up monitor and go to sleep until some other thread enters the same monitor and call notify. 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. Syntax: public final void wait() throws InterruptedException public final void wait(long timeout)throws InterruptedException notify() : Wakes up a thread that called wait() on same object. 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() notifyAll() :Wakes up all the thread that called wait() on same object. Wakes up all threads that are waiting on this object's monitor. Syntax: public final void notifyAll() InterThreadDemo CODE class InterThreadDemo OUTPUT Available Balance 10000 DESCRIPTION In the above program we have shown threads communicate and we have shown how to wait and notify methods. In the above program we have two classes THINGS TO TRY
|