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 */
}
}
}
|