There are two ways to create a thread.

It can be created by extending the Thread class and overriding its run() method:

Extend Syntax

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

Another way to create a thread is to implement the Runnable interface:

Implement Syntax

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



Practice Excercise Practice now