Friday, 16 August 2013

multiThrading in Java

Java defines two ways To implement threads:


  • You can implement the Runnable interface.
  • You can extend the Thread class.
As we know if we use thread class then we can't extends any other class so inheritance is not allowed but on the flip side if we use Runnable interface then we extends any other class and use the feature of inheritance.So according to me Runnable interface is more used than Thread class.

1.Using Runnable interface

Here is the program:


class first implements Runnable{
public void run(){
for(int i=1;i<=10;i++)
{
System.out.println("value of i is"+i);
}
}
}
class second implements Runnable
{
public void run()
{
for(int j=1;j<=10;j++)
{

System.out.println("value of j is"+j);
}
}
}
class runables
{
public static void main(String args[])
{
first ms=new first();
Thread thread1=new Thread(ms);
thread1.start();

second obj2=new second();
Thread thread2=new Thread(obj2);
thread2.start();
    
}
}

Output of this program:

Multithreading


Like the Post? Do share with your Friends.

No comments:

Post a Comment

IconIconIconFollow Me on Pinterest

Blogger news

Blogroll

What's Hot