Home  >  Article  >  Java  >  How to create a Java thread class by inheriting the Thread class

How to create a Java thread class by inheriting the Thread class

WBOY
WBOYforward
2023-05-07 15:43:081096browse

Inherit the Thread class to create a thread class

Thread implementation steps:

  1. Define a subclass of Thread, override the run() method, run( ) method represents the task to be completed by the thread, and the run() method is called the thread execution body.

  2. Create an instance of the Thread subclass, and the subclass object is the thread.

  3. Call the start() method of the thread object to start the thread.

<code>public class ThreadDemo extends Thread{<br><br>    public void run() {<br>        for(int i=0;i<10;i++) {<br>            System.out.println(currentThread().getName()+":" + i);<br>        }<br>    }<br><br>    public static void main(String args[]) {<br>        new ThreadDemo().start();<br>        new ThreadDemo().start();<br>    }<br>}</code>

The above is the detailed content of How to create a Java thread class by inheriting the Thread class. For more information, please follow other related articles on the PHP Chinese website!

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