class SampleThread1 implements Runnable{
 Thread t;
 SampleThread1(String s) {
 t=new Thread(this,s);
 t.start();
}

public void run(){
for(int i=0;i<5;i++){

System.out.println("Thread Name :"+Thread.currentThread().getName());
try{
Thread.sleep(1000);
}catch(Exception e){}
}
}
}

public class RunnableThread1{
public static void main(String args[]){
System.out.println("Thread Name :"+Thread.currentThread().getName());
SampleThread1 m1=new SampleThread1("My Thread 1");
SampleThread1 m2=new SampleThread1("My Thread 2");
}
}

output of the program is

 

Thread Name :main

Thread Name :My Thread 1

Thread Name :My Thread 2

Thread Name :My Thread 1

Thread Name :My Thread 2

Thread Name :My Thread 1

Thread Name :My Thread 2

Thread Name :My Thread 1

Thread Name :My Thread 2

Thread Name :My Thread 1

Thread Name :My Thread 2

 




Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)