Creating Subclasses of Thread

Creating Subclasses of Thread

Previous Home Next

 

The first way to specify what code a thread is to run, is to create a subclass of Thread and override the run() method. The run() method is what is executed by the thread after you call start().



 

To create subclass of thread first extends the subclass from the base class thread.like this
public class MyThread extends Thread

 

public class MyThread extends Thread {

public void run(){
System.out.println("MyThread running");
}
}

To create and start the above thread you can do like this:

MyThread myThread = new MyThread();

myTread.start();



Previous Home Next