Home> Java> javaTutorial> body text

In Java, can we override start() method?

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

In Java, can we override start() method?

Yes, we canoverridestart()method of Thread class in Java. We must call thesuper.start()method to create a new thread, and we need to call therun()method in the newly created thread. If we call therun()method directly from thestart()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");  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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!