Introduction of thread class

Create thread class

Java uses java.lang.Threadclasses to represent threads , and all thread objects must be instances of the Thread class or its subclasses. The role of each thread is to complete a certain task, in fact it is to execute a section of program flow, that is, a section of code that is executed sequentially. Java uses thread execution to represent this program flow.

**Main thread: **The thread that executes the main method
Single-threaded program : There is only one thread in the java program. The execution starts from the main method and executes from top to bottom. The JVM executes the main method, and the main method enters the stack. Memory, the JVM will find the operating system to open up a path of execution from the main method to the cpu, and the cpu can execute the main method through this path, and this path has a name, called main (main) thread.
Examples are as follows:

public class Test01MainThread {
    
    
    public static void main(String[] args) {
    
    
        Person p1 = new Person("小强");
        p1.run();
        System.out.println(0/0);//ArithmeticException: / by zero
        Person p2 = new Person("小明");
        p2.run();
    }
}

//当出现异常以后,因为是单线程,小明的method不能执行。

operation result:
operation result

·Multi-threading can improve this problem

The steps to create and start multithreading in Java by inheriting the Thread class are as follows:

1. 定义Thread类的子类,并重写该类的run()方法,该run()方法的方法体就代表了线程需要完成的任务,因此把run()方法称为线程执行体。
2. 创建Thread子类的实例,即创建了线程对象
3. 调用线程对象的start()方法来启动该线程

A way to create a multi-threaded program: create a subclass of the Thread class
java.lang.Thread class: is a class that describes threads, we want to implement multi-threaded programs, we must inherit the Thread class

实现步骤:
    1.创建一个Thread类的子类
    2.在Thread类的子类中重写Thread类中的run方法,设置线程任务(开启线程要做什么?)
    3.创建Thread类的子类对象
    4.调用Thread类中的方法start方法,开启新的线程,执行run方法
         void start() 使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
         结果是两个线程并发地运行;当前线程(main线程)和另一个线程(创建的新线程,执行其 run 方法)。
         多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。
java程序属于抢占式调度,那个线程的优先级高,那个线程优先执行;同一个优先级,随机选择一个执行
public class Test01Thread {
    
    
    public static void main(String[] args) {
    
    
        //3.创建Thread类的子类对象
        MyThread mt = new MyThread();
        //4.调用Thread类中的方法start方法,开启新的线程,执行run方法
        mt.start();
        for (int i = 0; i < 3; i++) {
    
    
            System.out.println("main:"+"方法执行"+i);
        }
        }
    }

Class creation:

	//1.创建一个Thread类的子类
public class MyThread extends Thread{
    
    
    //2.在Thread类的子类中重写Thread类中的run方法,设置线程任务(开启线程要做什么?)
    @Override
    public void run() {
    
    
        
          for (int i = 0; i < 3; i++) {
    
    
            System.out.println("run:"+"执行");
        }
        
    }
}

Result:
Insert picture description here
Insert picture description here
Note: Due to the priority of each execution, the order in which the methods appear is different.

Guess you like

Origin blog.csdn.net/cena1001/article/details/105162244