Create thread using java.lang.Thread



class MyThread extends Thread {
 public MyThread(String name) {
  super(name);
 }

 @Override
 public void run() {
  System.out.println("\nBegin Work");
  try {
   System.out.println("Thread is working...");
  } catch (Exception e) {
   System.out.println("Error while running thread. Reason: "
     + e.getMessage());
  }
  System.out.println("Work Finished");
 }
}

public class ThreadFlight {
 public static void main(String[] array) {
  MyThread myThread = new MyThread("I am a Thread");
  
  System.out.println("Name: " + myThread.getName());
  System.out.println("Priority: " + myThread.getPriority());
  System.out.println("State: " + myThread.getState());
  System.out.println("Id: " + myThread.getId());
  
  myThread.start();
 }
}
 
Output
Name: I am a Thread
Priority: 5
State: NEW
Id: 8

Begin Work
Thread is working...
Work Finished
 
To create a thread using thread class, c class need to extends java.lang. Thread class and override the run() method. run() method does not return a value also it does not throw exception. To make the thread working call the start() method on the created thread object. It will call the overriden run() method. Though you can also call the run() method directly but it will not be thread at that time. It will behave like a normal object calling a method.