Start Coding Now

Multithreading in Java - Thread Class & Runnable

Get started with multithreading in Java. How to create and run threads using Thread class and Runnable interface.

Multithreading

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program.

Creating Threads

1. Extend Thread Class:

public class Main extends Thread {
  public void run() {
    System.out.println("This code is running in a thread");
  }
}

2. Implement Runnable Interface:

public class Main implements Runnable {
  public void run() {
    System.out.println("This code is running in a thread");
  }
}

Running Threads

Main thread = new Main();
thread.start();

Frequently Asked Questions