Turn Desktop View Off
by Dinesh Thakur

Multithreading replaces event loop programming by dividing the tasks into discrete and logical units. Threads also helps in avoiding polling. Polling is usually implemented by a loop that is used to check some condition repeatedly. Once the condition is true, appropriate action is taken, this results in wastage of CPU time. Suppose that the producer has to wait until the consumer is finished before it generates more data. In a polling system, the consumer would waste many CPU cycles while it waits for the producer to produce. Once the producer has finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on.

To avoid polling, Java includes an elegant interprocess communication mechanism via the wait(), notify(); and notifyAll() methods. These methods are implemented as final methods in Object. All three methods can be called only from within a synchronized context.

1 wait()        tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify()ยท

2 notify()       wakes up the first thread that called wait() on the same object.

3 notifyAll()  wakes up all the threads that called wait() on the same object. The highest priority thread will run first.

class resource

                    {
                       boolean ok=false;
                   public synchronized void disp(String id,String[] h)
                        {
                           try
                               {
                             while(!ok)
                                  {
                                     wait();
                                  }
                                   }
                                     catch(InterruptedException e){}
                                            ok=false;
                                    for(int i=0;i<h.length;i++)
                                        {
                                       System.out.println(id+":"+h[i]);
                                    }
                        }
                                                   public synchronized void allow()
                                          {
                                             ok=true;
                                             notify();
                                          }
                   }
                                                         class InterThread extends Thread
                                                              {
                                                         String s[]={"Welcome","to","Java"};
                                                         resource r;
                                                         String name;
                                                         InterThread(String n,resource x)
                                                               {
                                                              super(n);
                                                              name=n;
                                                              this.r=x;
                                                              System.out.println("Thread"+getName());
                                                           }
                                                                          public void run()
                                                                  {
                                                                 r.disp(name,s);
                                                                  }
                                                             }
                  
                              class InterthreadCommunication
                                {
                               public static void main(String args[])
                                  {
                                          resource m=new resource();
                                      InterThread t1=new InterThread("First Thread",m);
                                      InterThread t2=new InterThread("Second Thread",m);
                                      t1.start();
                                      t2.start();
                                      while(t1.isAlive()|| t2.isAlive())
                                        {
                                        try
                                           {
                                             Thread.sleep(100);
                                           }
                                             catch(InterruptedException e) {}
                                                  m.allow();
                                         }
                                      }                   
                               }     

Interthread Communication in Java Example