Object源码阅读

打印 上一主题 下一主题

主题 915|帖子 915|积分 2745

Object源码阅读

native:本地栈方法,使用C语言中实现的方法。
  1. package java.lang;
  2. public class Object {
  3.         //注册本地方法
  4.     private static native void registerNatives();
  5.     static {
  6.         registerNatives();
  7.     }
  8.     //返回Object对象的class
  9.     public final native Class<?> getClass();
  10.     //根据Object对象的内存地址计算hash值
  11.     public native int hashCode();
  12.     //比较两个对象内存地址是否相同
  13.     public boolean equals(Object obj) {
  14.         return (this == obj);
  15.     }
  16.     //浅拷贝:指针的拷贝,指针指向同一内存地址,如:Object obj1 = obj;
  17.     //深拷贝:对象的拷贝
  18.     //对象拷贝,深拷贝(内存地址不同)
  19.     protected native Object clone() throws CloneNotSupportedException;
  20.     //返回类名+@对象内存地址求hash再转16进制
  21.     public String toString() {
  22.         return getClass().getName() + "@" + Integer.toHexString(hashCode());
  23.     }
  24.     //唤醒一个等待object的monitor对象锁的线程
  25.     public final native void notify();
  26.     //唤醒所有等待object的monitor对象锁的线程
  27.     public final native void notifyAll();
  28.     //让当前线程处于等待(阻塞)状态,直到其他线程调用此对象的 notify()  方法或 notifyAll() 方法,或者超过参数 timeout 设置的超时时间
  29.     public final native void wait(long timeout) throws InterruptedException;
  30.     //nanos是额外时间,超时时间为timeout + nanos
  31.     public final void wait(long timeout, int nanos) throws InterruptedException {
  32.         if (timeout < 0) {
  33.             throw new IllegalArgumentException("timeout value is negative");
  34.         }
  35.         if (nanos < 0 || nanos > 999999) {
  36.             throw new IllegalArgumentException(
  37.                                 "nanosecond timeout value out of range");
  38.         }
  39.         if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
  40.             timeout++;
  41.         }
  42.         wait(timeout);
  43.     }
  44.     //方法让当前线程进入等待状态。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法
  45.     public final void wait() throws InterruptedException {
  46.         wait(0);
  47.     }
  48.     //GC确定不存在对该对象的更多引用,回收时会调用该方法
  49.     protected void finalize() throws Throwable { }
  50. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

杀鸡焉用牛刀

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表