java.lang.Object

來源:互聯網
上載者:User

package java.lang;

/**
 *
 * Object是所有對象的父類,對象包括一般對象和數組對象
 *
 * comment by liqiang
 *
 */
public class Object {

    private static native void registerNatives();
    static {
        registerNatives();
    }

    /**
     *
     * 獲得當前對象對應的Class對象
     * 得到 Class對象的方式主要有三種
     * 1 object.getClass()
     * 2 String.class;
     * 3 Class clazz = Class.forName("aa.bb");
     * 
     * @return  當前對象對應的Class對象
     *
     */
    public final native Class getClass();

    /**
     *
     * 返回當前對象的hashCode
     * 注意: 1同一對象多次調用此方法得到的值是一樣的
     *      2兩個對象如果調用equals方法返回true,則他們的hashCode也應相同
     *      3如果兩個對象調用equals方法返回false則他們那的hashCode不一定不同
     * 當前的hashCode值是由記憶體位址轉換成的一個int值
     *
     *
     * @return  hashCode值
     *
     */
    public native int hashCode();

    /**
     *
     * 判斷兩個對象是否相等
     *
     * 特性:
     * x不為null
     * 1 reflexive(自反性) x.equals(x)=true恒成立
     * 2 symmetric(對稱性) x.equals(y)=true成立,則y.equals(x)=true成立
     * 3 transitive(傳遞性) 如果x.equals(y)=true,y.equals(z)=true成立,則x.equals(z)=true成立
     * 4 consistent(一致性) 如果x.equals(y)=true,則無論調用多少次都返回true
     * 5 x.equals(null)為false
     *
     * Object的預設實現是比較記憶體位址
     * 如果子類重寫了此方法,則最好重寫hashCode方法,保證兩個對象相等則hashCode也相等
     *
     * @param   obj   進行比較的對象
     * @return  相等返回true不等返回false
     *
     */
    public boolean equals(Object obj) {
 return (this == obj);
    }

    /**
     *
     * 返回clone對象,調用此方法必須繼承Cloneable介面
     * 否則拋出CloneNotSupportedException
     *
     */
    protected native Object clone() throws CloneNotSupportedException;

    /**
     *
     * 對象的String表示
     *
     * @return  對象的String表示.
     *
     */
    public String toString() {
 return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

    /**
     *
     * 此方法隨意喚醒一個等待對象A的鎖的線程,使其重新有權爭奪A的鎖
     * 同一時間只有一個線程能擁有一個對象的鎖
     * 一個線程能擁有一個對象的鎖是在1調用同步方法,2同步塊,3靜態同步方法,注意靜態同步方法
     * 是對Class對象加鎖
     * watie,notify必須在鎖定一個對象A時調用
     * 被喚醒線程只有在當前線程釋放掉對象的鎖時(同步方法結束,或同步塊結束)才開始競爭對象的鎖,所以
     * 雖然線程調用了notify方法但是不立即釋放鎖
     *
     * @exception  如果當前線程不具有對象A的鎖則拋出IllegalMonitorStateException
     */
    public final native void notify();

    /**
     *
     * 喚醒所有等待對象A的鎖的線程,這些線程將會競爭此鎖
     *
     * @exception  如果當前線程不具有對象A的鎖則拋出IllegalMonitorStateException
     *
     */
    public final native void notifyAll();

    /**
     * 使當前線程進入對象A的等待隊列(waite set),線程將被掛起,並釋放掉此對象的鎖,但是擁有的
     * 其他對象的鎖在掛起狀態還會保持,這也是產生死結的原因
     *
     * 線程在一下情況會被喚醒
     * 1其他擁有此對象的鎖的線程調用notify方法,且正好此線程被喚醒,
     * 2其他擁有此對象的鎖的線程調用notifyAll
     * 3時間到期
     * 4其他線程中斷此線程
     * 然後他線程爭奪此線程的鎖
     *
     * 當時間為0時無限等待
     *
     *
     * @param      timeout   等待的毫秒數
     * @exception  等待數為負則拋出IllegalArgumentException
     * @exception  如果線程沒有獲得對象的鎖拋出IllegalMonitorStateException
     * @exception  此線程在waite時被另外的線程打斷,則拋出InterruptedException 當此異常拋出時
     *       此線程的中斷標誌將被清除
     */
    public final native void wait(long timeout) throws InterruptedException;

    /**
     *
     * 等待指定時間
     *
     */
    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);
    }

    /**
     *
     * 在記憶體回收前調有此方法,為了清除資源
     * 在finalize方法中拋出的異常將會使回收中斷,但是異常將被忽視
     *
     * @throws Throwable the <code>Exception</code> raised by this method
     */
    protected void finalize() throws Throwable { }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.