search
HomeJavaJavagetting StartedImplement java threads by inheriting the Thread class

Implement java threads by inheriting the Thread class

May 30, 2020 pm 04:28 PM
javathreadthread

Implement java threads by inheriting the Thread class

The Thread class is the parent class of all thread classes and implements the extraction and encapsulation of threads.

The specific steps to inherit the Thread class to create and start multi-threads are:

1. Define a class, inherit from the Thread class, and override the run method of the class, and the method of the run method The body represents the task that the thread needs to complete. Therefore, the method body of the run method is called the thread execution body.

2. Create an object of Thread subclass, that is, create a child thread.

3. Use the start method of the thread object to start the thread.

(Video tutorial recommendation: java video)

Example:

Demo first creates a ticket sales thread

package demo1;

public class SellTickets extends Thread {
    //共享数据
    static int count = 100;
    @Override
    public void run() {
        //循环售票
        while(count > 0) {
            count--;
            System.out.println(Thread.currentThread().getName() + "售出了一张票,剩余" + count);
        }
    }
}

Test class

import demo1.SellTickets;

public class TheadDemo {

    public  static void main(String[] args) {
        //模拟四个售票员售票
        SellTickets s1 = new SellTickets();
        SellTickets s2 = new SellTickets();
        SellTickets s3 = new SellTickets();
       // System.out.println(s1.currentThread().getName());  //这个线程的名称是main
        s1.start();
        s2.start();
        s3.start();

    }
}

Test results:

Implement java threads by inheriting the Thread class

Recommended tutorial: java entry program

The above is the detailed content of Implement java threads by inheriting the Thread class. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools