我看JAVA 之 Object & JNI

  1. 云栖社区>
  2. 博客>
  3. 正文

我看JAVA 之 Object & JNI

awenhaowenchao 2019-12-06 17:12:49 浏览955
展开阅读全文

我看JAVA 之 Object & JNI

注:基于jdk11

Object

Object类是java语言中所有类的父类。


public class Object {
    //注册本地函数,方可以从库中的本机代码调用JNI函数
    private static native void registerNatives();
    static {
        registerNatives();
    }

    /**
     * Constructs a new object.
     */ 
    //HotSpotIntrinsicCandidate注解,说明本方法,jvm额外维护了一个利用了CPU指令的高效实现
    @HotSpotIntrinsicCandidate
    public Object() {}

    //返回对象的运行时类型
    /**
     * Number c = 0;
     * Class<? extends  Number> clazz = c.getClass();
     * System.out.println(clazz);
     * 打印如下:
     * class java.lang.Integer
     * 关于泛型及泛型擦除概念,见后续泛型章节
     */
    @HotSpotIntrinsicCandidate
    public final native Class<?> getClass();

     /**
     * 返回对象的hash码,hashCode方法支持hash表(比如java.util.HashMap)的特性,
     * 不同的对象返回唯一的has码,可以提高hash表数据结构的性能。
     * hashCode有时候以对象内存地址的机制生成,有时候不是
     * java.lang.System.identityHashCode 工具方法 返回默认hashCode()方法的值,null引用返回的hash码为0
     */ 
    @HotSpotIntrinsicCandidate
    public native int hashCode();

    /**
    * 标示其他对象与当前对象是否相等
    * 自反性: 自己(非null)与自己比较永远相当
    * 对称性: x vs y or y vs x 等价
    * 可传递: x.equals(y) == true and y.equals(z), then x.equals(z)
    * 一致性: 一旦x.equals(y) == true, then anytime x.equals(y) == true
    * any not null value not equals null
    * 如果覆盖equals方法,一定要覆盖hashCode方法
    */
    public boolean equals(Object obj) {
        return (this == obj);
    }

    /**
     * clone返回会返回一个相同类型的新对象拷贝,如果要实现克隆,那么当前类及其父类需要实现cloneable接口。
     * 对于复杂类型,默认的clone方式仅实现”浅克隆”,如果要实现”深克隆”,需要覆盖clone方法。
     * 注:在软件编程中,推荐使用工具类的方式做copy,而不是覆盖clone的方式
          */
    @HotSpotIntrinsicCandidate
    protected native Object clone() throws CloneNotSupportedException;

    /**
    * 默认返回 getClass().getName() + '@' + Integer.toHexString(hashCode())
    * 具体类可以覆盖Object的toString()方法
    */
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

    /**
     * 持有该对象锁的线程通过调用notify唤醒等待此对象监视器的某一个线程,选择是随机的(具体由不同虚拟机实现)。一次只能有一个线程持有对象锁。
     * 获取对象锁定三种方式如下:
     * 1,通过执行该对象的同步实例方法
     * 2,通过执行该对象的同步代码块
     * 3,通过执行类的同步静态方法
     * 见 /openjdk/hotspot/src/share/vm/prims/jvm.cpp line: 526~530
     * 见 /openjdk/hotspot/src/share/vm/runtime/synchronizer.cpp line: 407~411
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @see        java.lang.Object#notifyAll()
     * @see        java.lang.Object#wait()
     */
    @HotSpotIntrinsicCandidate
    public final native void notify();

    /**
     * 持有该对象锁的线程通过调用notify唤醒等待此对象监视器的所有线程。当当前线程释放对象监视器后,其他唤醒的线程会竞争对象监视器。
     *
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @see        java.lang.Object#notify()
     * @see        java.lang.Object#wait()
     */
    @HotSpotIntrinsicCandidate
    public final native void notifyAll();

    /**
     * 调用此方法会导致当前对象进入等待状态,知道被其他线程唤醒(notify)或中断(InterruptedException)。相当于wait(0L)
     *
     * @throws IllegalMonitorStateException if the current thread is not
     *         the owner of the object's monitor
     * @throws InterruptedException if any thread interrupted the current thread before or
     *         while the current thread was waiting. The <em>interrupted status</em> of the
     *         current thread is cleared when this exception is thrown.
     * @see    #notify()
     * @see    #notifyAll()
     * @see    #wait(long)
     * @see    #wait(long, int)
     */
    public final void wait() throws InterruptedException {
        wait(0L);
    }

    /**
     * 
     * @param  timeoutMillis the maximum time to wait, in milliseconds
     * @throws IllegalArgumentException if {@code timeoutMillis} is negative
     * @throws IllegalMonitorStateException if the current thread is not
     *         the owner of the object's monitor
     * @throws InterruptedException if any thread interrupted the current thread before or
     *         while the current thread was waiting. The <em>interrupted status</em> of the
     *         current thread is cleared when this exception is thrown.
     * @see    #notify()
     * @see    #notifyAll()
     * @see    #wait()
     * @see    #wait(long, int)
     */
    public final native void wait(long timeoutMillis) throws InterruptedException;

    /**
     *  在并发编程章节重点讲解
     * @param  timeoutMillis the maximum time to wait, in milliseconds
     * @param  nanos   additional time, in nanoseconds, in the range range 0-999999 inclusive
     * @throws IllegalArgumentException if {@code timeoutMillis} is negative,
     *         or if the value of {@code nanos} is out of range
     * @throws IllegalMonitorStateException if the current thread is not
     *         the owner of the object's monitor
     * @throws InterruptedException if any thread interrupted the current thread before or
     *         while the current thread was waiting. The <em>interrupted status</em> of the
     *         current thread is cleared when this exception is thrown.
     * @see    #notify()
     * @see    #notifyAll()
     * @see    #wait()
     * @see    #wait(long)
     */
    public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
        if (timeoutMillis < 0) {
            throw new IllegalArgumentException("timeoutMillis value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos > 0) {
            timeoutMillis++;
        }

        wait(timeoutMillis);
    }

    /**
     * 通知垃圾回收器当前对应已经没有引用了,具体回收还要看jvm内部实现。
     * jdk9 标记为已过时,在jvm原理章节重点讲解
     *
     * @throws Throwable the {@code Exception} raised by this method
     * @see java.lang.ref.WeakReference
     * @see java.lang.ref.PhantomReference
     * @jls 12.6 Finalization of Class Instances
     */
    @Deprecated(since="9")
    protected void finalize() throws Throwable { }
}

JNI(Java Native Interface)

java本地接口,意为JAVA语言提供一套规范供操作系统底层实现,一般是C/C++语言,不同操作系统版本的jvm提供了相应的实现,以达到"Write Once Run Anywhere“

实现JNI大致步骤如下

  1. 定义java程序

    package chapter01;
    
    public class TestJNI {
    
        public native void hello();//所有native关键词修饰的都是对本地的声明
        static {
            System.loadLibrary("hello");//载入本地库
        }
        public static void main(String[] args) {
            new TestJNI().hello();
        }
    }
  2. 编译javac chapter01/TestJNI.java,生成TestJNI.class
  3. 执行javac chapter01/TestJNI.java -h . ,生成JAVA本地接口chapter01_TestJNI.h

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class chapter01_TestJNI */
    
    #ifndef _Included_chapter01_TestJNI
    #define _Included_chapter01_TestJNI
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     chapter01_TestJNI
     * Method:    hello
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_chapter01_TestJNI_hello
      (JNIEnv *, jobject);
    
    #ifdef __cplusplus
    }
    #endif
    #endif

    注意:JNIEnv、 jobject等类型都是在jni.h头文件中定义的,所以需要include jni.h

  4. 编写步骤3的接口实现,创建TestJNIImpl.c

    #include "jni.h"
    #include "chapter01_TestJNI.h"
     
    //#include otherheaders
     
    JNIEXPORT void JNICALL
    Java_chapter01_TestJNI_hello(JNIEnv *env, jobject obj)
    {
        printf("Helloworld!\n");
        return;
    }
  5. 将本地方法编写的文件生成动态链接库

    gcc -dynamiclib -I /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include TestJNIImpl.c -o libhello.jnilib

    注意:如果/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include不存在jni_md.h的话,可以从 /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include/darwin 拷贝过来

  6. 执行java程序

    java -Djava.library.path=/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include chapter01/TestJNI

网友评论

登录后评论
0/500
评论
awenhaowenchao
+ 关注