Creating threads

Java provides a class Thread that represents a thread. Threads can be created with the help of this class. The signature of the run() method is given by the Runnable interface. This interface is assumed by the Thread class.

You basically have the choice of deriving from the Thread class and overriding its run() method, or implementing the Runnable interface and then specifying the executable object when creating a thread. The run() method has no parameters, so required data must be passed elsewhere. For example, via attributes of the object that implements the Runnable interface.

By deriving the class Thread

public class MyThread extends Thread {
	public void run() {
		System.out.println("Thread is running");
	}
}

By implementing the runnable interface

public class MyThread implements Runnable {
	public void run() {
		System.out.println("Thread is running");
	}
}