Monday, 19 January 2015

Write JAVA thread Program to find length of String.

File Name=RunnableDemo

import java.util.Scanner;
class RunnableDemo1 implements Runnable
{
   private Thread t;
   private String threadName;
   String s;
   RunnableDemo1(String name)
   {
   Scanner in = new Scanner(System.in);
   threadName = name;
   System.out.println("Enter String ");
   s = in.nextLine();
   }

  public void run()
{
        try {
                Thread.sleep(5000);
                System.out.println("length Of Thread " +  s.length());
            }
            catch (InterruptedException ex)
            {
               System.out.println("Exception "+ex');
            }
   }

   public void start ()
   {
         t = new Thread (this, threadName);
         t.start ();
   }
}

public class RunnableDemo
{
   public static void main(String args[])
  {
      RunnableDemo1 R1 = new RunnableDemo1("Thread-1");
      R1.start();
   }
}

    

No comments:

Post a Comment