Multi-threaded code implementation

Multi-threaded code implementation method 1

        code show as below

package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wuyulin
 * Date: 2023-07-22
 * Time: 14:01
 */
//多线程编程方法1
class HelloThread extends Thread{
    @Override
    public void run() {
//        System.out.println("hello thread");
        while (true){
            System.out.println("hello thread");
            try {
                Thread.sleep(1000); //休眠多少毫秒
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Demo1 {
    public static void main(String[] args) {
        HelloThread helloThread=new HelloThread();
        //调用start()方法相当于开启一个新的线程,会调动操作系统创建线程的api,在系统内核中将线程对应的pcb给创建出来并管理好
        //新的线程执行的程序便是run()方法中的程序
        helloThread.start();
        //直接调用run方法就相当于调用一个普通的方法,就不会调用操作系统创建线程的api,不会创建出一个线程
//        helloThread.run();
        while (true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

        The above implements a multi-threaded program

        main points

                The main point of the code is to create a class that inherits the Thread class, rewrite the run() method in it, and the run() method is the program to be executed by the thread. After rewriting the run() method, instantiate a A specific object, and then the object calls the start () method to create a specific thread to run the program in the run () method, and then completes the simplest multi-threaded program.

                Many of the above codes are used to test and observe multi-threading. In the run() method, we wrote an infinite loop to print hello thread. In the main method, we wrote an endless loop to print hello mian, and passed Thread. sleep (1000) to sleep for 1000 milliseconds to facilitate our observation. In practice, main and helloThread.start() are two threads, and main is the main thread. We run the above program and observe that printing hello mian and hello thread are executed alternately. Here it is reflected that the two printing operations are two threads

         doubt

                Some people may have such doubts, can I call the run() method directly, so after practice, you will find that hello thread can also be printed directly through helloThread.run(), but the printing situation becomes like this up:

 Now it will only print hello thread but not hello mian, indicating that there is no multi-threading, because calling the run() method directly is equivalent to calling a normal method, although it is true that the code in it can be executed, but it will not create a thread , so there is only the main thread of main, so after entering the infinite loop, the position of printing hello mian cannot be reached at all, and the operation of calling the start() method can call the "create thread" API provided by the operating system at the bottom layer, and at the same time, it will be in the operation The corresponding pcb structure is created in the kernel of the system and added to the corresponding linked list. At this time, the newly created thread will participate in the CPU scheduling. The work to be performed by this thread is the run( )method.

Multi-threaded code implementation method 2

        code show as below:

package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wuyulin
 * Date: 2023-07-22
 * Time: 18:36
 */
//多线程编程方法2
class HelloRunnable implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("hello runnable");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Demo2 {
    public static void main(String[] args) {
        HelloRunnable helloRunnable=new HelloRunnable();
        Thread thread=new Thread(helloRunnable);
        thread.start();
        while (true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

        main points

                Create a class HelloRunnable that implements the Runnable interface, and rewrite the run() method in the HelloRunnable class. The program in the run() method is the program to be executed by the thread. In the main() method, instantiate a class that implements the Runnable interface. Class HelloRunnable, then pass it into the constructor of Thread as a parameter, instantiate a Thread object, and then call the start() method through the Thread class object to create a process (because only the start() method is defined in the Thread class, So the object of the Thread class can call the start() method)

Guess you like

Origin blog.csdn.net/q322359/article/details/131871547