How to create a thread in Java

Java 8Object Oriented ProgrammingProgramming

A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called.

The Main thread in Java is the one that begins executing when the program starts. All the child threads are spawned from the Main thread and it is the last thread to finish execution.

A program that demonstrates this is given as follows:


 Live Demo

class ThreadDemo implements Runnable {
   Thread t;
   ThreadDemo() {
      t = new Thread(this, "Thread");
      System.out.println("Child thread: " + t);
   public void run() {
      try {
         System.out.println("Child Thread");
      } catch (InterruptedException e) {
         System.out.println("The child thread is interrupted.");
      System.out.println("Exiting the child thread");
public class Demo {
   public static void main(String args[]) {
      new ThreadDemo();
      try {
         System.out.println("Main Thread");
      } catch (InterruptedException e) {
         System.out.println("The Main thread is interrupted");
      System.out.println("Exiting the Main thread");


Child thread: Thread[Thread,5,main]
Main Thread
Child Thread
Exiting the child thread
Exiting the Main thread
Updated on 30-Jul-2019 22:30:24