Object源碼解析

/**
 * java類繼承體系下的根類,是所有類的超類基類,所有的對象都擁有該類的所有方法
 *
 * @author  unascribed
 * @see     java.lang.Class
 * @since   JDK1.0
 */
public class Object {

    /**
     * Returns the runtime class of this {@code Object}. The returned
     * {@code Class} object is the object that is locked by {@code
     * static synchronized} methods of the represented class.
     *
     * <p><b>The actual result type is {@code Class<? extends |X|>}
     * where {@code |X|} is the erasure of the static type of the
     * expression on which {@code getClass} is called.</b> For
     * example, no cast is required in this code fragment:</p>
     *
     * <p>
     * {@code Number n = 0;                             }<br>
     * {@code Class<? extends Number> c = n.getClass(); }
     * </p>
     *
     * @return The {@code Class} object that represents the runtime
     *         class of this object.
     * @jls 15.8.2 Class Literals
     * 返回對象運行時類
     */
    public final native Class<?> getClass();

    /**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hash tables such as those provided by
     * {@link java.util.HashMap}.
     * <p>
     * The general contract of {@code hashCode} is:
     * <ul>
     * <li>Whenever it is invoked on the same object more than once during
     *     an execution of a Java application, the {@code hashCode} method
     *     must consistently return the same integer, provided no information
     *     used in {@code equals} comparisons on the object is modified.
     *     This integer need not remain consistent from one execution of an
     *     application to another execution of the same application.
     * <li>If two objects are equal according to the {@code equals(Object)}
     *     method, then calling the {@code hashCode} method on each of
     *     the two objects must produce the same integer result.
     * <li>It is <em>not</em> required that if two objects are unequal
     *     according to the {@link java.lang.Object#equals(java.lang.Object)}
     *     method, then calling the {@code hashCode} method on each of the
     *     two objects must produce distinct integer results.  However, the
     *     programmer should be aware that producing distinct integer results
     *     for unequal objects may improve the performance of hash tables.
     * </ul>
     * <p>
     * As much as is reasonably practical, the hashCode method defined by
     * class {@code Object} does return distinct integers for distinct
     * objects. (This is typically implemented by converting the internal
     * address of the object into an integer, but this implementation
     * technique is not required by the
     * Java&trade; programming language.)
     *
     * @return  a hash code value for this object.
     * @see     java.lang.Object#equals(java.lang.Object)
     * @see     java.lang.System#identityHashCode
     */
    public native int hashCode();

    /**
     * Indicates whether some other object is "equal to" this one.
     * 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>
     * Note that it is generally necessary to override the {@code hashCode}
     * method whenever this method is overridden, so as to maintain the
     * general contract for the {@code hashCode} method, which states
     * that equal objects must have equal hash codes.
     *
     * @param   obj   the reference object with which to compare.
     * @return  {@code true} if this object is the same as the obj
     *          argument; {@code false} otherwise.
     * @see     #hashCode()
     * @see     java.util.HashMap
     */
    public boolean equals(Object obj) {
        return (this == obj);
    }

    /**
     * 使用該方法需實現Cloneable接口
     */
    protected native Object clone() throws CloneNotSupportedException;

    /**
     * It is recommended that all subclasses override this method.
     */
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

    /**
     * 喚醒等待此對象監視器的單個線程
     * 同一時間只能有一個線程擁有對象的監視器
     *
     * @throws  IllegalMonitorStateException  如果當前線程不是該對象監視器的所有者
     * @see        java.lang.Object#notifyAll()
     * @see        java.lang.Object#wait()
     */
    public final native void notify();

    /**
     * 喚醒此對象監視器上等待的所有線程。線程通過調用其中一個等待方法來等待對象的監視器。
     * 在當前線程放棄對該對象的鎖定之前,喚醒的線程將無法繼續。喚醒的線程將以通常的方式與任何其他線程競爭,
     * 這些線程可能正在積極競爭以在此對象上同步;例如,喚醒的線程在成爲下一個鎖定此對象的線程時沒有可靠的特權或劣勢。
     *
     * @throws  IllegalMonitorStateException  如果當前線程不是該對象監視器的所有者
     * @see        java.lang.Object#notify()
     * @see        java.lang.Object#wait()
     */
    public final native void notifyAll();

    /**
     * 使當前線程等待,直到另一個線程調用此對象的notify()方法或notifyAll()方法,或者經過指定的時間。
     * 當前線程必須擁有此對象的監視器。
     *
     * 此方法會導致當前線程(稱爲t)將自身放置在此對象的等待集中,然後放棄對此對象的任何和所有同步聲明。
     * 出於線程調度的目的,線程t將被禁用,並處於休眠狀態,直到發生以下四種情況之一:
     *
     * 1.其他一些線程爲此對象調用notify方法,而線程t恰好被任意選擇爲要喚醒的線程。
     * 2.其他一些線程爲此對象調用notifyAll方法。
     * 3.另一個線程中斷線程t。
     * 4.指定的實時時間或多或少已經過去。但是,如果超時爲零,則不考慮實時性,線程只需等待通知。
     *
     * 然後從該對象的等待集中刪除線程t,並重新啓用線程調度。然後,它以通常的方式與其他線程競爭對象上的同步權;
     * 一旦它獲得了對象的控制權,它對該對象的所有同步聲明都將恢復到原來的狀態,也就是說,恢復到調用wait方法時的狀態。
     * 然後線程t從wait方法的調用返回。因此,從wait方法返回時,對象和線程t的同步狀態與調用wait方法時完全相同。
     *
     * @see @see 
     * 線程也可以在不被通知、中斷或超時的情況下喚醒,即所謂的“虛假喚醒”。雖然這種情況在實踐中很少發生,
     * 但應用程序必須通過測試本應導致線程被喚醒的條件,並在條件不滿足時繼續等待來防範這種情況。
     * 換句話說,等待應該總是以循環的形式出現,就像這樣:
     * @see @see
     * 
     * <pre>
     *     synchronized (obj) {
     *         while (&lt;condition does not hold&gt;)
     *             obj.wait(timeout);
     *         ... // Perform action appropriate to condition
     *     }
     * </pre>
     * 
     * (For more information on this topic, see Section 3.2.3 in Doug Lea's
     * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
     * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
     * Language Guide" (Addison-Wesley, 2001).
     *
     * <p>If the current thread is {@linkplain java.lang.Thread#interrupt()
     * interrupted} by any thread before or while it is waiting, then an
     * {@code InterruptedException} is thrown.  This exception is not
     * thrown until the lock status of this object has been restored as
     * described above.
     * 
     * 在wait期間,如果當前線程被任何其他線程interrupt,一個InterruptedException會被拋出,
     * 但是被拋出之前,必須先恢復獲取鎖之前的狀態,如果沒恢復鎖狀態,該異常不會拋出
     *
     * 請注意,wait方法在將當前線程放入此對象的等待集中時,僅解鎖此對象;
     * 在線程等待期間,可以同步當前線程的任何其他對象將保持鎖定狀態。
     *
     * 此方法只能由作爲此對象監視器所有者的線程調用。有關線程成爲監視器所有者的方式的描述,請參見notify方法。
     *
     *
     * @param      timeout   等待的最大毫秒數
     * @throws  IllegalArgumentException      if the value of timeout 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 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;

    /**
     * @see wait
     *
     * @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);
    }

    /**
     * @see wait
     *
     * @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 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 void wait() throws InterruptedException {
        wait(0);
    }

    /**
     * 此方法可被重寫
     * 永遠不要手動調用,應該由jvm自動決定是否調用
     * 最多隻會調用一次
     * 即使該方法報錯拋出異常,jvm也不做處理
     */
    protected void finalize() throws Throwable { }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章