Simple ways to create thread in java

You can create Thread in java in many different ways so you should learn all of these methods to start thread.
  1. Create a Class and Extend it from Thread class
  2. Implement Runnable Interface
  3. Using Anonymous class
now lets discus each of these methods with detail

1-Create a Class and Extend it from Thread class

This is the very first method that you can use to create thread , first create class and extend it from Thread class and override its Run() method and put your dirty code in run() that you want to run when you create new thread from this class. like this
class Runner extends Thread {

    @Override
    public void run() {
        
        // dirty code goes here
        
        for (int i = 0; i <= 10; i++) {
            System.out.println("Hello" + i);
        }
    }
}
and you can use this class just like this in your main code
 class Runner extends Thread {

    @Override
    public void run() {
        
        // dirty code goes here
        
        for (int i = 0; i <= 10; i++) {
            System.out.println("Hello " + i);
        }
    }
}

public class Main {

    public static void main(String[] args) {
        Runner runner1 = new Runner();
        runner1.start();
    }
}

2-Implement Runnable Interface

This method is similar to previous method , in this method instead of extending class from thread , implement Runnable interface with it and implement its run() method as like previous method.
class Runner implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i <= 10; i++) {
            System.out.println("Hello" + i);
        }
    }
}
and you can use  this  class like this
class Runner implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i <= 10; i++) {
            System.out.println("Hello" + i);
        }
    }
}

public class Main {

    public static void main(String[] args) {

        Thread t1 = new Thread(new Runner());
        t1.start();
    }
}

3-Using Anonymous class

If you do not want to create new class to start thread then you can use this method , in this method create object of  Thread class and in constructor pass object of Runnable  anonymous class. and start your thread by invoking start() method. just like this
public class Main {

    public static void main(String[] args) {

        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {

                for (int i = 0; i <= 10; i++) {

                    System.out.println("Hello" + i);

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        System.err.println(ex.getMessage());
                    }
                }
            }
        });
        t1.start();

    }

}

Output

output of all above three method is the same.
run:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
BUILD SUCCESSFUL (total time: 1 second)

Some other useful resources about creating thread in java

  1. Defining and Starting a Thread - Oracle
  2. How to create thread - Java Point
  3. Thread Start - Tutorials Point

Final words

I tried my best to make this article informative as much as i can and i did it. Now its your turn ,  try this code in your IDE and play with it , if i did not cover anything in it please comment below i will include it or if you have any question write it comment i will try to answer it. Happy Coding