Home > Java > javaTutorial > body text

Java -- multithreading

高洛峰
Release: 2016-11-21 11:09:07
Original
1890 people have browsed it

  Multi-threading implementation method:

  Inherit the Thread class

  Implement the Runnable class

 -------------------------------- -------------------------------------------------- ----

 1. Inherit the Thread class

  After inheriting the Thread class, you need to override the public void run() method of the parent class as the main method of the thread.

  The execution of all threads must be concurrent, that is: multiple threads will execute alternately in the same time period. In order to achieve this goal, you must not call the run() method directly. Instead, you should call the start() method of the Thread class to start multiple threads.

  Comparison of calling the start() method and calling the run() method:

public class MyThread extends Thread {
    private String name;
    public MyThread(String name) {
        this.name = name;
    }
    
    @Override
    public void run() {
        for(int i=0; i<10; i++) {
        System.out.println(name + "打印:" + i);
        }
    }
    
    public static void main(String[] args) {
        MyThread mt1 = new MyThread("线程A");
        MyThread mt2 = new MyThread("线程B");
        MyThread mt3 = new MyThread("线程C");
        mt1.start();
        mt2.start();
        mt3.start();
    }
}
Copy after login

Running results: (Three threads are executed simultaneously and alternately, there is no fixed execution order)

Java -- multithreading

public class MyThread extends Thread {
    private String name;
    public MyThread(String name) {
        this.name = name;
    }
    
    @Override
    public void run() {
        for(int i=0; i<5; i++) {
        System.out.println(name + "打印:" + i);
        }
    }
    
    public static void main(String[] args) {
        MyThread mt1 = new MyThread("线程A");
        MyThread mt2 = new MyThread("线程B");
        MyThread mt3 = new MyThread("线程C");
        mt1.run();
        mt2.run();
        mt3.run();
    }
}
Copy after login

Running results: (Three programs Execute in sequence)

Java -- multithreading

  2. The principle of multi-threading implemented by the start() method

  Open the start() method part of the Thread class source code:

public synchronized void start() {
        if (threadStatus != 0)
            throw new IllegalThreadStateException();
        group.add(this);
        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
            }
        }
}
private native void start0();
Copy after login

 The native keyword refers to the method of calling the operating system, start0 () method is the method of the operating system.

 Since the startup of threads involves the allocation of resources in the operating system, the startup of specific threads should be implemented differently according to different operating systems. The JVM implements different implementations according to the start0() method defined in different operating systems. In this way, the name of the start0() method does not change at the multi-thread level, and different operating systems have different implementations.

Java -- multithreading

Schematic diagram

Conclusion: Only the start() method of the Thread class can allocate operating system resources, so the way to start multi-threads is always the start() method of the Thread class.


Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!