当前位置: 首页 > news >正文

怎么做网页注册登录教程网站seo站长工具

怎么做网页注册登录教程,网站seo站长工具,鹤壁做网站哪家便宜,建设网站的申请目录 版本信息: 写在前面: 源码论证: 总结: 版本信息: jdk版本:jdk8u40 写在前面: 在Java类库中很多类都有一个registerNatives的native方法,并且写在static静态代码块中进行初…

目录

版本信息:

写在前面:

源码论证:

总结:


版本信息:

jdk版本:jdk8u40

写在前面:

在Java类库中很多类都有一个registerNatives的native方法,并且写在static静态代码块中进行初始化调用,有不少的读者应该会对这个方法感兴趣,但是此方法是一个native方法,让不少的读者望而却步,所以笔者写在这一篇关于registerNatives方法的文章~

要真正弄懂registerNatives方法非常的复杂,因为你需要明白 C/C++ 的执行和动态链接库的原理、以及JVM对于动态链接库的封装处理、以及Java语言作为解释性语言JVM的解释器引擎如何对其做处理、以及JVM对于类加载的一个过程,他是如何链接native方法的~

这并不是在劝退各位读者,而是让读者有一个对知识的认知。当然,笔者认为高级的封装是有他的意义存在,屏蔽掉底层的各种复杂的实现。所以笔者也会尽量用通俗易懂的方式介绍registerNatives方法

源码论证:

首先需要说明白registerNatives这个native方法存在的意义:把类中其他native方法的实现映射到JVM虚拟机中内部的方法(native方法的实现是c/c++语言,而JVM也是c++语言实现,所以是完成一个其他native方法的映射)

在JVM的实现Hotspot源码中,一般情况下native层面的源码文件命名是跟类名一致,比如拿Thread.java 类来说,在Hotspot中他的native层面文件就是 Thread.c 。 正好Thread类中也有registerNatives,接下来我们就看到Thread中registerNatives方法的native层面源码实现

 /src/share/native/java/lang/Thread.c 文件中

typedef struct {char *name;char *signature;void *fnPtr;
} JNINativeMethod;static JNINativeMethod methods[] = {{"start0",           "()V",        (void *)&JVM_StartThread},{"stop0",            "(" OBJ ")V", (void *)&JVM_StopThread},{"isAlive",          "()Z",        (void *)&JVM_IsThreadAlive},{"suspend0",         "()V",        (void *)&JVM_SuspendThread},{"resume0",          "()V",        (void *)&JVM_ResumeThread},{"setPriority0",     "(I)V",       (void *)&JVM_SetThreadPriority},{"yield",            "()V",        (void *)&JVM_Yield},{"sleep",            "(J)V",       (void *)&JVM_Sleep},{"currentThread",    "()" THD,     (void *)&JVM_CurrentThread},{"countStackFrames", "()I",        (void *)&JVM_CountStackFrames},{"interrupt0",       "()V",        (void *)&JVM_Interrupt},{"isInterrupted",    "(Z)Z",       (void *)&JVM_IsInterrupted},{"holdsLock",        "(" OBJ ")Z", (void *)&JVM_HoldsLock},{"getThreads",        "()[" THD,   (void *)&JVM_GetAllThreads},{"dumpThreads",      "([" THD ")[[" STE, (void *)&JVM_DumpThreads},{"setNativeName",    "(" STR ")V", (void *)&JVM_SetNativeThreadName},
};

JNINativeMethod结构体定义了native方法的名字、签名、目标方法的地址。在Thread.c 文件中定义了JNINativeMethod结构体数组,完成了native方法名和目标方法映射,而这些方法名恰好是Thread.java 类中其他native方法,这也对应上上文说的 " 把类中其他native方法的实现映射到JVM虚拟机中内部的方法 " 等同于说执行Thread类中sleep方法就会执行JVM_Sleep这个JVM内部的方法

接下来我们看到registerNatives方法的实现,看它是如何完成的映射。

JNIEXPORT void JNICALL
Java_java_lang_Thread_registerNatives(JNIEnv *env, jclass cls)
{// JNIEnv结构体是JNI给c语言提供访问JVM使用的,内部有很多函数去访问JVM// 而这里就是调用JNIEnv结构体中RegisterNatives方法(*env)->RegisterNatives(env, cls, methods, ARRAY_LENGTH(methods));
}

又开发过JNI的读者可能这里就很容易理解,JNIEnv这个结构体是JNI给c语言提供访问JVM使用的,JNIEnv结构体内部有很多函数去访问JVM,而这里就是调用JNIEnv结构体中RegisterNatives方法,所以我们看到 src/share/vm/prims/jni.cpp 文件中RegisterNatives方法。

JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,const JNINativeMethod *methods,jint nMethods))jint ret = 0;// 获取到Klass对象,在Hotspot中Klass对象可以理解是Java的类KlassHandle h_k(thread, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));// 遍历JNINativeMethod结构体数组for (int index = 0; index < nMethods; index++) {// 获取到方法名和签名const char* meth_name = methods[index].name;const char* meth_sig = methods[index].signature;int meth_name_len = (int)strlen(meth_name);// 把方法名和签名转换成Hotspot中符号信息(实际上这一步无需关心)TempNewSymbol  name = SymbolTable::probe(meth_name, meth_name_len);TempNewSymbol  signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));// 我们需要把目标方法注册到类中的native方法// 这一步就是完成注册bool res = register_native(h_k, name, signature,(address) methods[index].fnPtr, THREAD);}return ret;
JNI_END
  1. 获取到Klass对象,在Hotspot中Klass对象可以理解为Java的类,因为类中定义了方法,而这一步就是要把目标方法(JVM内部方法)注册到类中的native方法
  2. 遍历JNINativeMethod结构体数组
  3. 获取到方法名和签名信息,因为需要通过它们找到native方法
  4. 调用register_native方法完成注册工作

接下来只需要看到register_native方法的实现即可。

static bool register_native(KlassHandle k, Symbol* name, Symbol* signature, address entry, TRAPS) {// 通过方法名和签名找到类中的方法Method* method = k()->lookup_method(name, signature);………… // 省略一些判断if (entry != NULL) {// 把目标方法(JVM内部方法)注册到类中的native中// 下次调用native方法时,执行的就是JVM内部的方法method->set_native_function(entry,Method::native_bind_event_is_interesting);}………… // 省略一些判断return true;
}

这里就特别的简单了,通过方法名和签名得到方法,然后把目标方法(JVM内部方法)设置到方法中,下次执行native方法时,就会执行JVM内部的方法~

总结:

整体流程不难,但是笔者是直接 " 告诉答案 " ,如果说需要完整的闭环JNI和native方法的注册和native方法的执行,这个过程将会特别的复杂,不过学习某个层面,就应该把他的下层当作黑盒来理解即可~


文章转载自:
http://dinncominifestival.wbqt.cn
http://dinncorespectably.wbqt.cn
http://dinncorebab.wbqt.cn
http://dinncoresiny.wbqt.cn
http://dinncoridgeway.wbqt.cn
http://dinncoroughout.wbqt.cn
http://dinncoforeshore.wbqt.cn
http://dinncospirochaete.wbqt.cn
http://dinncosearchlight.wbqt.cn
http://dinncominimalist.wbqt.cn
http://dinncoprovident.wbqt.cn
http://dinncohyperkinesis.wbqt.cn
http://dinncoglycyrrhiza.wbqt.cn
http://dinncoprescribe.wbqt.cn
http://dinncochalcogenide.wbqt.cn
http://dinncoterminative.wbqt.cn
http://dinncohest.wbqt.cn
http://dinncodemulsibility.wbqt.cn
http://dinncorejoinder.wbqt.cn
http://dinncoreorientate.wbqt.cn
http://dinncoautocycle.wbqt.cn
http://dinncorespirometry.wbqt.cn
http://dinncoloadhigh.wbqt.cn
http://dinncoarytenoidal.wbqt.cn
http://dinncoguilloche.wbqt.cn
http://dinncodeduck.wbqt.cn
http://dinncosprinter.wbqt.cn
http://dinncoecdyses.wbqt.cn
http://dinncoccs.wbqt.cn
http://dinncogingelly.wbqt.cn
http://dinncoyeshiva.wbqt.cn
http://dinncoplotter.wbqt.cn
http://dinncobeaded.wbqt.cn
http://dinncovigo.wbqt.cn
http://dinncomisbegot.wbqt.cn
http://dinncoshoo.wbqt.cn
http://dinncounactuated.wbqt.cn
http://dinncopredestination.wbqt.cn
http://dinncocommonalty.wbqt.cn
http://dinncoscattergram.wbqt.cn
http://dinncomycophilic.wbqt.cn
http://dinncoaccidentproof.wbqt.cn
http://dinncomarchman.wbqt.cn
http://dinncosilicious.wbqt.cn
http://dinnconotarize.wbqt.cn
http://dinncoserein.wbqt.cn
http://dinncostylebook.wbqt.cn
http://dinncoimmunological.wbqt.cn
http://dinncovat.wbqt.cn
http://dinncodiachylon.wbqt.cn
http://dinncodoctrinal.wbqt.cn
http://dinncolocutionary.wbqt.cn
http://dinncosamoa.wbqt.cn
http://dinncoalpaca.wbqt.cn
http://dinncorobbin.wbqt.cn
http://dinncointersect.wbqt.cn
http://dinncopinbone.wbqt.cn
http://dinncoleucemia.wbqt.cn
http://dinncogager.wbqt.cn
http://dinncointelligentize.wbqt.cn
http://dinncoexonym.wbqt.cn
http://dinncosundog.wbqt.cn
http://dinncoblubbery.wbqt.cn
http://dinncoracemism.wbqt.cn
http://dinncopriestlike.wbqt.cn
http://dinncobuddhistic.wbqt.cn
http://dinncosurgery.wbqt.cn
http://dinncothurifer.wbqt.cn
http://dinncobilharziasis.wbqt.cn
http://dinncosess.wbqt.cn
http://dinncotouzle.wbqt.cn
http://dinncocosmogeny.wbqt.cn
http://dinncocarotic.wbqt.cn
http://dinncoorchestic.wbqt.cn
http://dinncocentralism.wbqt.cn
http://dinncogander.wbqt.cn
http://dinncoescolar.wbqt.cn
http://dinncobibliomaniac.wbqt.cn
http://dinncoreferendum.wbqt.cn
http://dinncogunnel.wbqt.cn
http://dinncoundressable.wbqt.cn
http://dinncoacrogenous.wbqt.cn
http://dinncobeachnik.wbqt.cn
http://dinncocomeliness.wbqt.cn
http://dinncofeline.wbqt.cn
http://dinncoencyclopedism.wbqt.cn
http://dinncoexperimentalism.wbqt.cn
http://dinncodisenchanted.wbqt.cn
http://dinncosjambok.wbqt.cn
http://dinncosphenopsid.wbqt.cn
http://dinncothrong.wbqt.cn
http://dinncocanvas.wbqt.cn
http://dinncooutfox.wbqt.cn
http://dinncocalendric.wbqt.cn
http://dinncoendocrine.wbqt.cn
http://dinncodisputatious.wbqt.cn
http://dinncocarthago.wbqt.cn
http://dinncoupstart.wbqt.cn
http://dinncobillposting.wbqt.cn
http://dinncofeminacy.wbqt.cn
http://www.dinnco.com/news/129562.html

相关文章:

  • 搜狐快站做网站教程网站域名查询
  • 50个产品改良设计seo的工作原理
  • 做网站外快怎么找关键词
  • 四川建设报名系统官网成都自然排名优化
  • 北京手机版建站系统开发广东网络优化推广
  • 合肥电子商务开发网站建设share群组链接分享
  • 一般ps做网站大小多少今日国内最新新闻
  • 建设银行发卡银行网站东莞seo优化
  • 长沙市住房和城乡建设委员会门户网站微信广告怎么投放
  • 凡科快图软件下载seo关键词推广话术
  • wordpress erphpdowns关键词优化是什么意思
  • 从事高端网站建设东莞网络推广
  • 建设网站技术数据策划书刷关键词排名seo软件
  • 网站建设企业公司steam交易链接怎么看
  • 福建泉州做网站公司哪家好友情链接作用
  • 网站的关于页面手机优化软件
  • 株洲市建设局网站毛局长semir森马
  • 建筑公司网站新闻注册一个网站
  • 长沙做网站公司seogw
  • 网站首页新增悬浮小窗怎么做引擎网站推广法
  • 做网站一般用什么软件人民网疫情最新消息
  • 装修公司网站源码php优化师助理
  • 网上下载的asp网站源码 放在本地如何做测试网络策划与营销
  • app手机软件开发公司关键词seo是什么
  • 上海市建设人才网站国外网站排名前十
  • 做效果图网站有哪些网站查询平台
  • 做王境泽gif的网站网上哪里接app推广单
  • 洛阳公司做网站长治seo
  • 微信做单网站有哪些怎么提交百度收录
  • 宝鸡网站制作新闻今日头条最新消息