JDK源码阅读之一-----Object
package java.lang;

/**
 *
 * Object类是所有类的超类
 */
public class Object {
    /* getClass、hashCode都是native方法,顺便在这里复习一下native方法吧,简单的讲一个native方法
       就是一个java调用非java代码的接口
    */
    private static native void registerNatives();
    static {
        registerNatives();
    }
    
    public final native Class<?> getClass();

    public native int hashCode();
    
    // equals方法,是用==号写的,在这里使用equals方法和使用==是一样的,所以要重写equeals方法
    public boolean equals(Object obj) {
	return (this == obj);
    }

    
    protected native Object clone() throws CloneNotSupportedException;

   
    public String toString() {
	return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

    // notify、notifyAll、wait这些操作线程的方法使劲上是Ojbect里的方法
    public final native void notify();

    public final native void notifyAll();
     * @param      timeout   the maximum time to wait in milliseconds.
     * @exception  IllegalArgumentException      if the value of timeout is
     *		     negative.
     * @exception  IllegalMonitorStateException  if the current thread is not
     *               the owner of the object's monitor.
     * @exception  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.
     * @see        java.lang.Object#notify()
     * @see        java.lang.Object#notifyAll()
     */
    public final native void wait(long timeout) throws InterruptedException;
     * @param      timeout   the maximum time to wait in milliseconds.
     * @param      nanos      additional time, in nanoseconds range
     *                       0-999999.
     * @exception  IllegalArgumentException      if the value of timeout is
     *			    negative or the value of nanos is
     *			    not in the range 0-999999.
     * @exception  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @exception  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 >= 500000 || (nanos != 0 && timeout == 0)) {
	    timeout++;
	}
	wait(timeout);
    }

    public final void wait() throws InterruptedException {
	wait(0);
    }

    protected void finalize() throws Throwable { }
}

阅读更多
个人分类: JAVA&JDK源码阅读
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

JDK源码阅读之一-----Object

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭