Monday 19 January 2015

JAVA Program to implement Simple Thread program

File name = RunnableDemo.java 


class RunnableDemo1 implements Runnable
{
   private Thread t;
   private String threadName;
   RunnableDemo1(String name)
   {
   threadName = name;
   System.out.println("Thread Is Start"+  threadName );
   }
  public void run()
  {
        try {
                Thread.sleep(5000);
                System.out.println("Thread Is Running " +  threadName );
                Thread.sleep(5000);
                System.out.println("length Of Thread " +  threadName.length());
            }
            catch (InterruptedException ex)
            {
                System.out.println("Exception is  "+ex);
            }
        System.out.println("Thread Is Exit " +  threadName );
   }
   public void start ()
   {
         t = new Thread (this, threadName);
         t.start ();
System.out.println("Thread Is Create "+  threadName );
   }
}

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


No comments:

Post a Comment