How to create a new Thread in Java / Various methods to create a Thread in Java

A little bit about thread

A thread is a piece of task which is to be executed in parallel to another task executed by an application.
Example, Consider a text editor which performs auto save operation after some particular length of text is typed into it.
Now there are two tasks, first showing the text typed into the editor and second, performing auto save.
Since the two tasks should be executed in parallel, they are performed in different threads. If both operations are done on the same thread, then while the thread is busy saving, it could not perform the typing process and it will show a lag till the save operation completes.

How to create Thread ?

There are a couple of ways of creating a thread.

I. Extending Thread class

Each thread is an instance of thread. So, a new thread may be created by extending Thread class. The class which extends Thread should have run method defined in it. Run method contains the task which is to be executed in thread. The new thread is started by calling start method. Refer code below:

public class NewThread extends Thread {
    public static void main(String[] args) {
	System.out.println("This is thread:\""+Thread.currentThread().getName()+"\"");
	NewThread thread = new NewThread ();
	//start new thread
	thread.start();
	}
 
    public void run() {
	System.out.println("This is thread:\""+Thread.currentThread().getName()+"\"");
	}
}

Above code outputs:

This is thread: “main”
This is thread: “Thread-0”

II. Implementing Runnable

Another way of creating a new thread is by making a class implement Runnable. Runnable is an interface in java.lang package which contains run method. When you implement Runnable, you have to provide definition of run method in your class and it is this run method which contains the task to be executed in a thread.
In order to start the thread we need to create an object of Thread class (or a class which extends Thread) and pass an object of this class (which implements Runnable) to the Thread class constructor as illustrated below:

public class NewTask implements Runnable {
 
  public static void main(String[] args) {
	System.out.println("This is thread:\""+Thread.currentThread().getName()+"\"");
	Thread ob = new Thread(new NewTask());
	ob.start();
  }
    public void run() {
	System.out.println("This is thread:\""+Thread.currentThread().getName()+"\"");
    }
}

Above code outputs:

This is thread: “main”
This is thread: “Thread-0”

Let’s tweak in:

  1. A thread represents a task which is to be executed along with some other tasks.
  2. Whether a class representing a thread extends Thread class or implements java.lang.Runnable, it needs to have a run() method defined.
  3. The signature of run method should be public void run() { }. If a class implements java.lang.Runnable or extends java.lang.Thread and provides a run method with a different signature such as public int run (String thread) { }, then it will be a compiler error.
  4. Access modifier of run method should be public. The reason behind this is simple. We are implementing run method of an interface which is implicitly public and we cannot reduce the visibility of a method while implementing / overriding.
  5. start() method is defined in  java.lang.Thread class and not in java.lang.Runnable interface.
  6. run() method is defined in java.lang.Runnable interface.
  7. You cannot call start() method directly on an instance of class which implements java.lang.Runnable while you can do this on class extending java.lang.Thread. What I mean is:
    public class MyClass implements Runnable {
     
    	public static void main(String[] args) {
              MyClass obj = new MyClass();
              obj.start(); //<strong>compiler error here</strong>
            }
    }

    Trying to call start on an instance of class implementing Runnable will give you

    The method start() is undefined for the type MyClass

  8. Reason for above is tweak no 5.
  9. java.lang.Thread class internally implements java.lang.Runnable.
  10. Starting java 8, java.lang.Runnable interface is a functional interface. An interface which has exactly one abstract method.