Java Native关键字 JNI

native:凡是带了native关键字的,说明java的作用范围达不到了,要回去调用底层c语言的库 会进入本地方法栈 调用本地方法接口JNI

JNI作用:扩展Java的使用 融合不同的编程语言为Java所用 最初是为了融合C、C++

在内存区域中专门开辟了一块标记区域 Native Method Stack 登记native方法 它在最终执行的时候通过JNI加载本地方法库中的方法

比如:

 new Thread(()->{
            
        },"aaa").start();

Thread中的start()方法里调用start0();
而start0()在Thread类中声明为private native void start0(); 是一个本地方法
可以看到这个start0 方法被 native 修饰着 。native 关键字告诉编译器(其实是JVM)调用的是该方法在外部定义,这里指的是C。java的源码里是找不到 start0()的。

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();
            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 */
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值