• JDK源码阅读--Object


    在java.lang包下
    Object类:是所有类的基类(父类)
    public final native Class<?> getClass();
    返回这个Object所代表的的运行时类

    public native int hashCode();//返回对象hash code
    如果是两个相同(指用=比较都为true的)的对象调用这个方法,会返回相同的Integer类型的hash码;
    如果是两个不相同(指用=比较都为false的)的对象调用这个方法,会返回不同的Integer类型的hash码;

    /**
    * The {@code equals} method for class {@code Object} implements
    * the most discriminating possible equivalence relation on objects;
    * that is, for any non-null reference values {@code x} and
    * {@code y}, this method returns {@code true} if and only
    * if {@code x} and {@code y} refer to the same object
    * ({@code x == y} has the value {@code true}).
    * <p>
    */
    public boolean equals(Object obj) {
    return (this == obj);
    }
    返回值表示两个对象是否"相同".true表示"相同",false表示"不相同"
    x.equals(x) 返回true
    非空引用x,y:
    如果x.equals(y) 返回true,那么y.equals(x) 也返回true
    具有传递性:
    非空引用x,y,z:
    如果 x.equals(y) 返回true, y.equals(z) 返回true,那么x.equals(z) 也返回true
    如果x非空:
    x.equals(null) 返回false
    参数obj代表要与之比较的对象,也就是要被比较de对象。
    当equals方法被重写的时候,必须也要重写hashCode方法,确保拥有相同的hash码。


    protected native Object clone() throws CloneNotSupportedException;
    返回一个复制的对象(注意浅复制、深复制)。
    调用这个方法的对象所在的类需要实现Cloneable接口才能使用到此方法,否则会抛出CloneNotSupportedException异常。


    public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    返回对象字符串的表现形式


    public final native void notify();
    唤醒单个线程,但是唤醒的线程不会立马继续执行,直到持有锁的线程释放这个锁,被唤醒的线程将和其他线程一同竞争去获得这个锁。


    public final native void notifyAll();
    唤醒所有线程,但是唤醒的线程不会立马继续执行,直到持有锁的线程释放这个锁,被唤醒的线程将和其他线程一同竞争去获得这个锁。

    public final native void wait(long timeout) throws InterruptedException;
    timeout的单位为 毫秒。
    让线程进入等待,直到另一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会停止等待。
    当前线程必须拥有对象的监视器(锁)
    如果当前线程在等待之前或等待期间被任何线程中断,则会抛出InterruptedException异常。
    如果当前线程不是对象监视器所拥有的,则抛出IllegalMonitorStateException异常。
    如果timeout是负数,则抛出IllegalArgumentException异常。


    /*@param timeout the maximum time to wait in milliseconds.
    * @param nanos additional time, in nanoseconds range
    * 0-999999.
    * @throws IllegalArgumentException if the value of timeout is
    * negative or the value of nanos is
    * not in the range 0-999999.
    * @throws IllegalMonitorStateException if the current thread is not
    * the owner of this object's monitor.
    * @throws InterruptedException if any thread interrupted the
    * current thread before or while the current thread
    * was waiting for a notification. The <i>interrupted
    * status</i> of the current thread is cleared when
    * this exception is thrown.
    */
    public final void wait(long timeout, int nanos) throws InterruptedException {
    if (timeout < 0) {
    throw new IllegalArgumentException("timeout value is negative");
    }

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

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

    wait(timeout);
    }
    nanos:额外的时间,以纳秒为单位,范围在0-999999.
    让线程进入等待,直到另一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会停止等待。
    timeout的单位为 毫秒。
    让线程进入等待,直到另一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会停止等待。
    当前线程必须拥有对象的监视器(锁)
    如果当前线程在等待之前或等待期间被任何线程中断,则会抛出InterruptedException异常。
    如果当前线程不是对象监视器所拥有的,则抛出IllegalMonitorStateException异常。
    如果timeout是负数,则抛出IllegalArgumentException异常。
    如果nanos范围越界,则抛出IllegalArgumentException异常。


    public final void wait() throws InterruptedException {
    wait(0);
    }
    这个方法就像在调用wait(0);
    让线程进入等待,直到另一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会停止等待。

    让线程进入等待,直到另一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会停止等待。
    当前线程必须拥有对象的监视器(锁)
    如果当前线程在等待之前或等待期间被任何线程中断,则会抛出InterruptedException异常。
    如果当前线程不是对象监视器所拥有的,则抛出IllegalMonitorStateException异常。


    protected void finalize() throws Throwable { }
    当确定不再有对该对象的引用时,垃圾回收时由对象上的垃圾回收器调用




































  • 相关阅读:
    centos 7 配置ip
    Linux下安装jmeter
    eclipse 高效快捷键大全
    eclipse中不能找到dubbo.xsd报错”cvc-complex-type.2.4.c“的 两种解决方法
    大型网站系统架构演化之路(转)
    程序员技术练级攻略
    JSP和servlet之间的传值(总结的很全面)
    正则表达式笔记
    cenos 安装nginx并添加到service
    mac os重装php
  • 原文地址:https://www.cnblogs.com/lixianyuan-org/p/10469091.html
Copyright © 2020-2023  润新知