Home > Java > javaTutorial > body text

In Java, can we override start() method?

王林
Release: 2023-08-20 18:17:28
forward
724 people have browsed it

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 ");
   }
}
Copy after login

Output

Overriding a start() method
run() method
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template