Inter-Thread communication

Inter-Thread communication:-
                           -synchronized method/block will provides an unordered mutual exclusivity b/w the thread to access the objects. Thats means if one thread start the execution of synchronized method then their is no certain time for the completion of method, upto that time the other thread have to wait. To overcome this problem we use the concept of inter Thread communication.

                                    In case of inter Thread communication the threads are capable to access the synchronized objects in the mutually exclusive manner in the ordered way that means any thread can release the lock during the execution of the synchronized method or block and moved into the waiting state.
        "java.lang.Object class" this class provides the following methods for inter thread communication-
1. wait()                                        2. notify()                                           3.notifyAll()

(1) wait()- always be invoked from synchronized method or block. it immediatly release the lock of the object and move the thread into the waiting state for the waiting of notification.

(2) notify()- will notify the thread that is currently in the waiting state by invoking the wait() with same object.

(3) notifyAll()- this method will notify all the threads that are currently in waiting state.

class Resource
{
       int data[]={1,2,3,4,5,6,7,8,9,10};
       boolean b;
       synchronized void update()
       {
             System.out.println("update() invoked.");
             try
             {
                   for(int i=0;i<=9;i++)
                   {
                          if(b)
                                      wait();
                          data[i]+=10;
                          System.out.println("updated value:"+data[i]);
                          notify();
                          b=true;
                   }
             }
             catch(Exception e)
             {}
       }
       synchronized void display()
       {
              System.out.println("display() invoked");
              try
              {
                       for(int i=0;i<=9;i++)
                       {
                               if(!b)
                                        wait();
                              System.out.println("value:"+data[i]);
                              notify();
                              b=false;
                       }
              }
              catch(Exception e)
              {}
       }
}

class MyThread extends Thread
{
         Resource r;
         MyThread(String name,Resource r)
         {
                 super(name);
                 this.r=r;
                 start();
         }
         public void run()
         {
               String name=getName();
               if(name.equals("first"))
                        r.update();
               if(name.equals("second"))
                       r.display();
         }
}

class WaitNotifyTest
{
     public static void main(String[] args)
     {
            Resource r=new Resource();
            new MyThread("first",r);
            new MyThread("second",r);
     }
}

each and every object has the monitor in which it maintains the references of the threads that are currently required the lock of object. it means all the threads in their stack currently any synchronized method or block currently loaded.

//notifyAllTest.java
class Resource
{
       int key;
       boolean b;
       void login()
       {
               String name=Thread.currentThread();
               System.out.println(name+" is starting login.");
               synchronized(this)
               {
                        try
                        {
                               System.out.println(name+" is going into waiting.....");
                               if(!b)
                                        wait();
                               System.out.println(name+" gets logged in with "+key+" key.");
                        }
                        catch(Excetpion e)
                        {
                        }
               }
       }
       synchronized void keyGen()
       {
                key=(int)(Math.random()*100);
                System.out.println(key+" key generated");
                b=true;
                notifyAll();
       }
}

class MyThread extends Thread
{
        Resource r;
        MyThread(Resource r,String name)
        {
              super(name);
              this.r=r;
              start();
        }
        public void run()
        {
               String name=getName():
               if(name.equals("admin"))
                           r.keyGen();
               else
                          r.login();
        }
}

class NotifyAllTest
{
         public static void main(String[] args)
         {
                  Resource r=new Resource();
                  new MyThread(r,"user1");
                  new MyThread(r,"user2");
                  new MyThread(r,"user3");
                  new MyThread(r,"admin");
                  new MyThread(r,"user4");
         }
}

Comments

Popular posts from this blog

define System.out.println()?

Reflection in java