Object原始碼閱讀
native:本地堆疊方法,使用C語言中實作的方法,
package java.lang;
public class Object {
//注冊本地方法
private static native void registerNatives();
static {
registerNatives();
}
//回傳Object物件的class
public final native Class<?> getClass();
//根據Object物件的記憶體地址計算hash值
public native int hashCode();
//比較兩個物件記憶體地址是否相同
public boolean equals(Object obj) {
return (this == obj);
}
//淺拷貝:指標的拷貝,指標指向同一記憶體地址,如:Object obj1 = obj;
//深拷貝:物件的拷貝
//物件拷貝,深拷貝(記憶體地址不同)
protected native Object clone() throws CloneNotSupportedException;
//回傳類名+@物件記憶體地址求hash再轉16進制
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
//喚醒一個等待object的monitor物件鎖的執行緒
public final native void notify();
//喚醒所有等待object的monitor物件鎖的執行緒
public final native void notifyAll();
//讓當前執行緒處于等待(阻塞)狀態,直到其他執行緒呼叫此物件的 notify() 方法或 notifyAll() 方法,或者超過引數 timeout 設定的超時時間
public final native void wait(long timeout) throws InterruptedException;
//nanos是額外時間,超時時間為timeout + nanos
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);
}
//方法讓當前執行緒進入等待狀態,直到其他執行緒呼叫此物件的 notify() 方法或 notifyAll() 方法
public final void wait() throws InterruptedException {
wait(0);
}
//GC確定不存在對該物件的更多參考,回收時會呼叫該方法
protected void finalize() throws Throwable { }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/554963.html
標籤:其他
下一篇:返回列表