/*Inter Thread Communication Threads are often interdependent - one thread depends on another thread to complete an operation, or to service a request The words wait and notify encapsulate the two central concepts to thread communication A thread waits for some condition or event to occur. You notify a waiting thread that a condition or event has occurred. wait(): This method causes the current thread to release the lock and wait until another thread invokes the notify() or notifyAll() method for the same object. It can also be used with a timeout to wait for a specified amount of time notify(): This method wakes up a single thread that is waiting on the object's monitor. The choice of which thread to wake up is arbitrary. notifyAll(): This method wakes up all the threads that are waiting on the object's monitor. Sleep (): The method sleep() is utilized to stop the execution of the current thread for some amount of time. It does not release any lock the thread has acquired or wait for other threads to signal it. */ class QueueClass { int number; synchronized int get( ) { System.out.println( "Got: " + number); return number; } synchronized void put( int number) { this.number = number; System.out.println( "Put: " + number); } } class Developer implements Runnable { QueueClass queueClass; Developer ( QueueClass queueClass) { this.queueClass = queueClass; } public void run( ) { int i = 0; for(int j=0; j<50; j++) { queueClass.put (i++); } } } class Client implements Runnable { QueueClass queueClass; Client (QueueClass queueClass) { this.queueClass = queueClass; } public void run( ) { for(int j=0; j<50; j++) { queueClass.get( ); } } } public class Caller { public static void main(String args[]) { QueueClass queueClass = new QueueClass( ); Developer Dev = new Developer(queueClass); Client cl = new Client (queueClass); Thread t1=new Thread(Dev); Thread t2=new Thread(cl); t1.start(); t2.start(); System.out.println("Press Control-C to stop"); } }