如何查看java中native方法的实现

如何查看java中native方法的实现


慕妹3242003
浏览 1914回答 1
1回答

猛跑小猪

下载JDK源代码啊,openJDK上有完整的JDK源代码,JDK源代码由C++、Java、C、汇编 这四种语言组成。JVM主体是C++写的,JNI部分是C,工具类是Java写的,JVM里混有汇编代码。路径:openjdk-7-fcs-src-b147\jdk\src\share\native\java\lang\System.c 找到这个/* Only register the performance-critical methods */static JNINativeMethod methods[] = {{"currentTimeMillis", "()J", (void *)&JVM_CurrentTimeMillis},{"nanoTime", "()J", (void *)&JVM_NanoTime},{"arraycopy", "(" OBJ "I" OBJ "II)V", (void *)&JVM_ArrayCopy},};方法的实现应该在JVM部分,路径openjdk-7-fcs-src-b147\hotspot\src\share,不同的操作系统实现不一样,应该是在对应的操作系统的包下,我在windows目录下找到了纳秒的实现jlong os::javaTimeNanos() {if (!has_performance_count) {return javaTimeMillis() * NANOS_PER_MILLISEC; // the best we can do.} else {LARGE_INTEGER current_count;QueryPerformanceCounter(¤t_count);double current = as_long(current_count);double freq = performance_frequency;jlong time = (jlong)((current/freq) * NANOS_PER_SEC);return time;}}看到了这个比较有趣的东西。。。the best we can do.再详细的懒得找了。我这没环境,没IDE,有环境的话,用IDE几下子就找到了。最终结论:performance_frequency,QueryPerformanceCounter;这个就是windows下的C API函数。至于这个函数,可以去google
打开App,查看更多内容
随时随地看视频慕课网APP