# Creating Threads in Java (with example)

In 
Published 2022-12-03

This tutorial will explain how we can create threads in Java.

Multithreading refers to two or more tasks executing concurrently within a single OS process. A thread is an independent path of execution within a process. Many threads can run concurrently within a process. There can be multiple processes inside the OS.

There are two ways to create thread in java:

  • by implementing the Runnable interface (java.lang.Runnable)
  • by extending the Thread class (java.lang.Thread)

Here are some examples of creating Threads in Java.

package com.exampe.java;

public class MyCustomThread extends Thread {

  String threadName;

  public MyCustomThread(String threadName) {
    this.threadName = threadName;
  }

  @Override
  public void run() {
    for (int i = 1; i <= 3; i++) {
      System.out.println("Message from Thread #"+threadName);
      try {
        Thread.sleep(200);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
    }
  }
}
package com.exampe.java;

public class MyCustomRunnableClass implements Runnable {

  String threadName;

  public MyCustomRunnableClass(String threadName) {
    this.threadName = threadName;
  }

  @Override
  public void run()
  {
    for (int i = 1; i <= 3; i++) {
      System.out.println("Message from Thread #"+threadName);
      try {
        Thread.sleep(200);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
    }
  }
}
package com.exampe.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) throws InterruptedException {

    SpringApplication.run(DemoApplication.class, args);

    System.out.println("--------------------------------------------");

    System.out.println("A Thread is not returning any value; it implements Runnable.");

    // Create a simple thread using lambda expression
    Thread t1 = new Thread(() -> {
      for (int i = 1; i <= 3; i++) {
        System.out.println("Message from Thread #1");
        try {
          Thread.sleep(200);
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
      }
    });

    // Create a thread using a custom thread definition
    Thread t2 = new MyCustomThread("2");

    // Create a thread using a custom "Runnable" class
    MyCustomRunnableClass myCustomRunnableClass = new MyCustomRunnableClass("3");
    Thread t3 = new Thread(myCustomRunnableClass);

    t1.start(); t2.start();t3.start();

    System.out.println("--------------------------------------------");
  }
}

And here is the result of the execution:

--------------------------------------------
A Thread is not returning any value; it implements Runnable.
Message from Thread #1
--------------------------------------------
Message from Thread #2
Message from Thread #3
Message from Thread #1
Message from Thread #2
Message from Thread #3
Message from Thread #2
Message from Thread #3
Message from Thread #1

Process finished with exit code 0