关闭

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

标签: jdkexceptionthreadobjectequalsjava
395人阅读 评论(0) 收藏 举报
分类:
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 { }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8834次
    • 积分:133
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:20篇
    • 译文:0篇
    • 评论:0条
    文章存档