Monday, 19 January 2015

Write JAVA Program to print string length using extended thread.

File Name = exteded_thread .java


import java.util.Scanner;
class demo1 extends Thread
{
   Thread t;
   String threadName,s;
   demo1(String name)
   {
       Scanner in = new Scanner(System.in);
       threadName = name;
       System.out.println("Creating Thread......");
       try
        {  
            Thread.sleep(1000);
        }
        catch(InterruptedException e)
        {
            System.out.println("Erroe is --> "+e);
        }
   }
   public void run()
   {
      System.out.println("Running Thread......");
      try
      {     Thread.sleep(1000);
            System.out.println("length Of String Is -->"+s.length());
            Thread.sleep(1000);
      }
      catch(InterruptedException e)
        {
            System.out.println("Erroe is --> "+e);
        }
     System.out.println("Thread Is exiting.");
   }
   public void start ()
   {
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}
public class exteded_thread
{  
    public static void main(String args[])
     {
      String s="Wellcome";
      demo1 T1 = new demo1(s);
      T1.start();
     }
}

No comments:

Post a Comment