【java】java.lang.Object类学习

学习一个比较parent的class吧。我什么时候能毕业啊。

package java.lang;

/**
 * Class Object is the root of the class hierarchy(层次结构)
 */
public class Object {

	private static native void registerNatives();

	static {
		registerNatives();
	}

	/**
	 * Returns the runtime class of an object. That Class
	 * object is the object that is locked by static synchronized 
	 * methods of the represented class.
	 PS:当static method需要synchronized修饰的时候,需要该object的Class
	 */
	public final native Class getClass();

	/**
	 * 常规协定:
	 * The general contract of hashCode is: 
	 * <li>Whenever it is invoked on the same object more than once during 
	 *     an execution of a Java application, the hashCode method 
	 *     must consistently return the same integer。
	 就是对同一object多次调用hashCode都返回一样的value
	 
	 * <li>如果2个object的equals方法判断相等,他们的hashCode一定相等
	 */
	public native int hashCode();

	/**
	一大堆的规定如自反性不想记忆,也记不住,总之就是那样
	主要是下面的Note:
	 * Note that it is generally necessary to override the hashCode
	 * method whenever this method is overridden, so as to maintain the
	 * general contract for the hashCode method, which states
	 * that equal objects must have equal hash codes. 
	 *
	必须override hashcode,参照上面的协定
	 */
	public boolean equals(Object obj) {
		return (this == obj);
	}

	/**protected修饰,需要override*/
	protected native Object clone() throws CloneNotSupportedException;

	public String toString() {
		return getClass().getName() + "@" + Integer.toHexString(hashCode());
	}

	/**
	 * 唤醒在this object 上wait的单个线程,从等待池转移到锁池。
	 这个线程需要重新获取object's monitor,然后同步
	 
	 如果有一车线程都在那等着,就像等公交一样,我擦。
	 那么JVM会选任意一个thread唤醒让她上车,要问选择的条件,我想谁好看就上谁吧
	 */
	public final native void notify();

	/**
	 * 唤醒在this object上wait的所有线程,这回公平了,都TMD给我上车吧,
	 看你们谁能最先拿到object's monitor,是抢占式的,不是时间片式的。
	 */
	public final native void notifyAll();

	/**
	 使当前线程等待,放弃monitor,进入等待池.如果另外一个线程调用了interrupted,则抛
	 String s = "javaeye";
	 synchronized(s){
	 	while(1==1){
	 		obj.wait();
	 	}
	 }
	 */
	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);
	}

	
	protected void finalize() throws Throwable {
	}
}

 

你可能感兴趣的:(java,jvm,thread)