環境:JDK 1.7 Eclipse java
Object 類源碼以下this
1 package java.lang; 2 3 public class Object { 4 5 private static native void registerNatives(); 6 static { 7 registerNatives(); 8 } 9 10 public final native Class<?> getClass(); 11 12 public native int hashCode(); 13 14 public boolean equals(Object obj) { 15 return (this == obj); 16 } 17 18 protected native Object clone() throws CloneNotSupportedException; 19 20 public String toString() { 21 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 22 } 23 24 public final native void notify(); 25 26 public final native void notifyAll(); 27 28 public final native void wait(long timeout) throws InterruptedException; 29 30 public final void wait(long timeout, int nanos) throws InterruptedException { 31 if (timeout < 0) { 32 throw new IllegalArgumentException("timeout value is negative"); 33 } 34 35 if (nanos < 0 || nanos > 999999) { 36 throw new IllegalArgumentException( 37 "nanosecond timeout value out of range"); 38 } 39 40 if (nanos >= 500000 || (nanos != 0 && timeout == 0)) { 41 timeout++; 42 } 43 44 wait(timeout); 45 } 46 47 public final void wait() throws InterruptedException { 48 wait(0); 49 } 50 51 protected void finalize() throws Throwable { } 52 }
Object 類中方法及說明以下:spa
1 registerNatives() //私有方法 2 getClass() //返回此 Object 的運行類。 3 hashCode() //用於獲取對象的哈希值。 4 equals(Object obj) //用於確認兩個對象是否「相同」。 5 clone() //建立並返回此對象的一個副本。 6 toString() //返回該對象的字符串表示。 7 notify() //喚醒在此對象監視器上等待的單個線程。 8 notifyAll() //喚醒在此對象監視器上等待的全部線程。 9 wait(long timeout) //在其餘線程調用此對象的 notify() 方法或 notifyAll() 方法,或 者超過指定的時間量前,致使當前線程等待。 10 wait(long timeout, int nanos) //在其餘線程調用此對象的 notify() 方法或 notifyAll() 方法,或者其餘某個線程中斷當前線程,或者已超過某個實際時間量前,致使當前線程等待。 11 wait() //用於讓當前線程失去操做權限,當前線程進入等待序列 12 finalize() //當垃圾回收器肯定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。 13
說明:線程
native
java關鍵字,Native Method 用以修飾非 java 代碼實現的方法(C || C++), 相似java調用非java代碼的接口。code