Translate

Thursday, June 9, 2016

Defining a thread in java

How to define a thread in java?


There are two ways to define a thread in java.

1.By implementing Runnable interface

2.By extending Thread class

1. By implementing Runnable interface

public class Mythread implements Runnable
{

public void run() {
System.out.print("thread executed");
}
public static void main(String[] args) {
Mythread mt=new Mythread();

Thread t1=new Thread(mt);

t1.start();
         
               System.out.print("main thread executed");
          
}

}


2.By extending Thread class




public class Mythread extends Thread
{

public void run() {
System.out.print("thread executed");
}
public static void main(String[] args) {
Mythread mt=new Mythread();
mt.start();
System.out.print("main thread executed");
}

}

No comments:

Post a Comment