JAVA multithreading learning (2) ---- JAVA realizes multithreading

The previous article is all about the basic understanding of multithreading. After understanding, continue to return to JAVA to realize multithreading

JAVA multi-threaded class: Thread
first look at thread.
Insert picture description here
Insert picture description here
Insert picture description hereInsert picture description here
As you can see from the above, there are two ways to create multi-threads:

  • Inherit Thread, rewrite the run method
  • Implement Runnable, rewrite the ruN method

Inherit Thread

public class Main extends Thread {
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 200; i++) {
    
    
            System.out.println("多多多多敲代码");
        }
    }
    public static void main(String[] args) {
    
    
        Main test = new Main();
        test.run();
        for (int i = 0; i < 2000; i++) {
    
    
            System.out.println("i = " + i);
        }
    }
}

//输出结果,先输出完200多多多多敲代码,再到2000次i
部分展示:
多多多多敲代码
多多多多敲代码
多多多多敲代码
多多多多敲代码
i = 0
i = 1
i = 2
i = 3
i = 4

However, if .start is executed alternately, the effect of more cycles is obvious

public class Main extends Thread {
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 200; i++) {
    
    
            System.out.println("多多多多敲代码");
        }
    }
    public static void main(String[] args) {
    
    
        Main test = new Main();
        test.start();
        for (int i = 0; i < 2000; i++) {
    
    
            System.out.println("i = " + i);
        }
    }
}
//输出结果,交替执行
部分展示:
多多多多敲代码
多多多多敲代码
多多多多敲代码
i = 1
多多多多敲代码
多多多多敲代码
多多多多敲代码

Implement the Runnable interface

public class TestThread implements Runnable{
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 20; i++) {
    
    
            System.out.println("多多多多敲代码");
        }
    }

    public static void main(String[] args) {
    
    
        TestThread testThread = new TestThread();
        //通过线程对象来开启我们的线程,代理
        //new Thread(testThread).run();
		new Thread(testThread).start();
        for (int i = 0; i < 2000; i++) {
    
    
            System.out.println("i = " + i);
        }
    }
}

运行结果与上面一致,不做过多的描述

Therefore, the following figure can be obtained
Insert picture description here

What is the difference between the above two implementations?

  1. Inherit the Thread class
    ①Subclass inherit the Thread class to have multi-threading ability
    ②Start method: subclass.start() ③Not
    recommended: Single inheritance limitations

  2. Realize Runnable interface
    ①Realize Runnable with multi-threading ability
    ②Start method: pass in the target object + Thread object.start() ③Recommendation
    : flexible and convenient, avoiding the limitation of single inheritance

Most of the content of the article is personally organized, refer to the article cited by java3y

Guess you like

Origin blog.csdn.net/weixin_42553616/article/details/107110498