Ads Sponsored By Google :)

Friday, 18 January 2013

runnable interface difference|vs extending thread class example in java


runnable interface in java runnable interface example in java java runnable interface runnable interface example runnable interface runnable vs thread runnable interface vs thread class runnable inteface vs extending thread class difference between runnable interface and thread class interface runnable runnable interface java


    Runnable Interface:

                         Runnable interface is one of the pre defined interface present in package 
   called Java.Lang.*. Runnable interface is an alternative mechanism for development of   
   multithreading applications. In some of the circumstances if derived class is already  
   extending some base class and at the same time extending Thread Class is not possible 
   because in java one derived class can extends only one base class to avoid this multiple 
   inheritance problem the derived class needs to implements Runnable interface
   Runnable interface contains only one method and whose prototype as follows:
   
   Public abstract void run():

             This method is used for defining the logic of the thread. run() method of Runnable  
   interface is abstract method and run() method of thread class is defined with null body.
   Industry is always recommended to override abstract run() of Runnable interface.

   For Example:
         Class thdemo implements Runnable
       {
            Public void run()
          {
              try{
                 for(int i=1;i<=10;i++)
                {
                   System.out.println(“value of i=”+i);
                   Thread.Sleep(1000);
                }
             catch(InterruptedException ie)
            {
              System.error.println(“problem in thread execution”);
            }
          }//run() method
    }//thdemo class
     
        Class thderive
      {
           Public static void main(String args[])
         {
             Runnable r1=new thdemo();
             Thread t1=new Thread(r1,”javajavax”);//Converting from Runnable reference
                                                                          //into Thread Class Object.
             System.out.println(“name of the thread “+t1.getName());
             System.out.println(“execution status of t1 thread before start =”+t1.isAlive());
             t1.start();
            System.out.println(“execution status of t1 after start=”+t1.isAlive());
         }
      }


    Difference Between Runnable Interface and Thread Class ?
                                                or 
   Why we are implementing Runnable interface even though we have Thread Class
   For creation of threads?

               If we want to use the applets and thread concepts as single combination, then by   
      extending applet class and Thread class into single user defined class(i.e: multiple   
      inheritance), is not supported by classes in java. By using interfaces we can overcome 
      multiple inheritance problem. So we are using Runnable interface by implementing into user 
      defined class for thread creation when multiple features are inheriting .

   The Following Example illustrate the concept of Runnable interface

    The following program implement scrolling message. By extending Thread class and  
   implementing Runnable Interface into user defined class.

      import java.awt.*;
      import java.applet.*;
      /*<applet code =”user” height=300 width=300> </applet>*/
     public class user extends Applet implements Runnable
    {
        String msg=”Welcome to JavaJavax”;
        public void init()
       {
        setBackground(color.CYAN);
        setForeground(color.RED);
        }
        public void start()
       {
       Thread t1=new Thread(this);//this refers to current class object which implements 

                                             //Runnable interface and converts into Thread Class object
         t1.start();
        }
       public void run()
      {
           try{
                 while(true)
                 {
                   char ch=msg.CharAt(10);
                   msg=msg.SubString(1,msg.length());
                   msg=msg+ch;
                   repaint();
                   Thread.Sleep(1000);
                  }
                }
          catch(InterruptedException e)
        {
          System.out.println(“Problem in thread Execution”);
         }
     }//run()
        public void paint(Graphics g)
      {
          Font f=new Font(“arial”,Font.BOLD,60)l
          g.SetFont(f);
          g.drawString(msg,100,100);
       }
    }//user class




No comments:

Post a Comment

LinkWithin