Creating A Thread

Two ways of creating Threads
  • Implementing Runnable interface.
  • Extending Thread class.
The Runnable interface has the following specification, comprising one method prototype declaration:
public interface Runnable 
{
	void run();
}
			
A thread, which is created based on an object that implements the Runnable interface, will execute the code defined in the public method run(). In other words, the code in the run() method defines an independent path of execution and thereby the entry and the exits for the thread. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.
The run() method of the Runnable object is eventually executed by the thread represented by the Thread object on which the start() method was invoked.

Example of Runnable interface :
class Demo implements Runnable 
{
	Thread t;
	Demo(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)
			{
				e.printStackTrace();
			}
		}
	}
}
public class ThreadDemo 
{
	public static void main(String[] args) 
	{
		new Demo("Thread");
		System.out.println("Thread name : 
			"+Thread.currentThread().getName()); 
			// will return main thread name
		Demo t1=new Demo("Thread 1");
		Demo t2=new Demo("Thread 2");
	}
}
	

A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.
Example of Thread class:
class MyThread extends Thread 
{
	MyThread() 
	{
    	// Create a new, second thread
		super("Demo Thread");
		System.out.println("Child thread: " + this);
		start(); // Start the thread
	}
	// This is the entry point for the second thread.
	public void run() 
	{
    	try 
    	{
        	for(int i = 5; i > 0; i--) 
        	{
            	System.out.println("Child Thread: " + i);
				// Let the thread sleep for a while.
            	Thread.sleep(500);
			}
      	}
      	catch (InterruptedException e) 
      	{
        	System.out.println("Child interrupted.");
		}
		System.out.println("Exiting child thread.");
	}
}
public class TestThread 
{
	public static void main(String args[]) 
	{
    	new MyThread(); // create a new thread
		try 
		{
        	for(int i = 5; i > 0; i--) 
        	{
				System.out.println("Main Thread: " + i);
				Thread.sleep(1000);
			}
		}
		catch (InterruptedException e) 
		{
        	System.out.println("Main thread interrupted.");
		}
		System.out.println("Main thread exiting.");
	}
}

Runnable vs Thread

Thread
Runnable
Thread is a class that is use to create threads. Runnable is a interface that is use to create threads.
When we extends Thread class, after that we can't extend any other class which you required(As java doesn't support multiple inheritance). When we implements Runnable, we can save a space for your class to extend any other class in future.
When we extends Thread class, each of our thread creates unique object and associate with it. When we implements Runnable, it shares the same object to multiple threads.