Example of thread using Runnable Interface

Here we explain how can create a thread using the Runnable Interface. For there we take three java classes-ThreadA, ThreadB and Runnabletest. ThreadA and ThreadB class implement the Runnable interface and Runnabletest class use for test both thread class ThreadA and ThreadB.
Now we write each class one bye and show the output.

ThreadA.java

package com.javaforecast4u;
 
class ThreadA implements Runnable
{
   public void run()
  {
    for(int i=0;i<5;i++)
           {
             System.out.println("Thread A = "  +i);
           }
  }
}

ThreadB.java

package com.javaforecast4u;

class ThreadB implements Runnable
{
       public void run()
        {
           for(int j=0;j<5;j++)
             {
               System.out.println("Thread B = "  +j);
             }
        }
}

Runnabletest.java

package com.javaforecast4u;

class Runnabletest
{
  public static void main(String args[])
      {
         ThreadA obj1=new ThreadA();
         Thread  threadA= new Thread (obj1);
         threadA.start();
         ThreadB obj2=new ThreadB();
                     Thread  threadB=new Thread(obj2);
                     threadB.start();
}
}

Output:

Thread A = 0
Thread A = 1
Thread A = 2
Thread B = 0
Thread A = 3
Thread A = 4
Thread B = 1
Thread B = 2
Thread B = 3
Thread B = 4

2 comments :

  1. Hello Arvind,
    output you had posted here that is not fixed. So better to avoid post output here.

    Thanks
    Vikas

    ReplyDelete
  2. Ok thanks for feedback.....

    ReplyDelete