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 { }
}