Home > Java > javaTutorial > 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
Release: 2023-05-07 15:43:08
forward
1134 people have browsed it

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>
Copy after login

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!

Related labels:
source:yisu.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