Native

suaxi
2020-11-29 / 0 评论 / 44 阅读 / 正在检测是否收录...

编写一个多线程启动类

package com.sw.lesson;

/**
 * @Author suaxi
 * @Date 2020/11/29 15:56
 */
public class Demo01 {
    public static void main(String[] args) {
        new Thread(() ->{

        },"Thread01").start();
    }
}

查看start方法的源码

    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0(); //调用了start0方法
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

    private native void start0();
    private native void start0();
    /*
        native:凡是带了native关键字的,说明java的作用范围达不到了,会去调用底层C语言的库
        进入本地方法栈
        调用本地接口  JNI(Java Native Interface)
        JNI的作用:扩展Java的使用,融合不同的编程语言为Java所用
        在内存中专门开辟一块标记区域:Native Method Stack,登记 native方法
        在最终执行的时候,加载本地方法库中的方法通过JNI
     */
0

评论 (0)

取消