Home  >  Article  >  Java  >  In Java, can we override start() method?

In Java, can we override start() method?

王林
王林forward
2023-08-20 18:17:28723browse

In Java, can we override start() method?

Yes, we can override start() method of Thread class in Java. We must call the super.start() method to create a new thread, and we need to call the run() method in the newly created thread. If we call the run() method directly from the start() method, it will be executed as a normal method in the actual thread, not in a new thread.

Example

public class ThreadTest {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
   }
}
class MyThread extends Thread {
   @Override
   public void start() { // overriding the start() method
      System.out.println("Overriding a start() method");
     <strong> </strong>super.start();
   }
   @Override
   public void run() {
      System.out.println("run() method ");
   }
}

Output

Overriding a start() method
run() method

The above is the detailed content of In Java, can we override start() method?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete