๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Create Threads in Java  


Introduction
  • Thread creation means preparing and starting a unit of execution so it can run concurrently with other code.
  • In Java, we create a thread-capable task and start it so the JVM scheduler can run its run() logic.
  • Different ways to create threads in Java:
    1. Extending Thread class.
    2. Implementing Runnable interface.
  • Both approaches are explained in depth below.

1. Create a Thread by Extending Thread class:
Steps are as follows:
  • Define the task:
    • Create a class that extends Thread and override run() with the work to perform.
    • class MyThread extends Thread
      {
          @Override
          public void run()
          {
              System.out.println("Thread task is executed...");
          }
      }
  • Create an Instance of Thread class:
    • Instantiate the new thread class within the main method.
    • MyThread t1 = new MyThread(3);
  • Start the thread:
    • Call start() (not run()) to move to Runnable state and then Running state.
    • t1.start(); // schedules thread to run concurrently
    • NOTE : If we directly call run() instead of start(), it runs as a normal method, not as a separate thread.
Full Program:
  • class MyThread extends Thread
    {
        @Override
        public void run()
        {
            System.out.println("Thread task is executed...");
        }
    }
    public class MainApp
    {
        public static void main(String[] args)
        {
            MyThread t = new MyThread();
            t.start();
        }
    }

2. Create a Thread by Implementing Runnable interface:
Steps are as follows:
  • Define the task:
    • Create a class that implements Runnable and override run() with the work to perform.
    • class MyRunnable implements Runnable
      {
          @Override
          public void run()
          {
              System.out.println("Thread task is executed...");
          }
      }
  • Create an Instance of Runnable and pass it to Thread:
    • Instantiate the Runnable class and pass it to a Thread object.
    • MyRunnable r = new MyRunnable();
      Thread t1 = new Thread(r);
  • Start the thread:
    • Call start() on the Thread object to move it into the Runnable and then Running state.
    • t1.start(); // schedules thread to run concurrently
    • NOTE : If we directly call run() on the Runnable, it executes like a normal method inside the current thread.
Full Program:
  • class MyRunnable implements Runnable
    {
        @Override
        public void run()
        {
            System.out.println("Thread task is executed...");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            MyRunnable r = new MyRunnable();
            Thread t = new Thread(r);
            t.start();
        }
    }

Use of start() and run() methods:
  • start() method:
    • It creates a new thread and automatically calls the run() method in a separate call stack.
    • It allows true multithreading, so multiple threads can run concurrently without blocking the main thread.
  • run() method:
    • It defines the task or logic that the thread will execute when it is started.
    • If directly called, it runs in the same thread, not creating a new thread.

Which is Better Way to Create Threads in Java ?
  • Runnable interface is generally preferred for real-world projects because:
    • Allows extending another class.
    • Separates task logic from thread execution.
    • Supports thread reusability (same Runnable object can be used for multiple threads).
  • Thread class is mostly used for quick examples or simple threads where inheritance is not an issue.