java.lang.Object作为所有类的基础类,java默认所有类都是Object的子类
静态块
static {
registerNatives();
}
属性:
native方法:
静态方法:
private static native void registerNatives();
1、获取类名
public final native Class> getClass();
2、获取hashCode,不同对象实例具有相同的hashCode,Object类的hashCode()方法返回这个对象存储的内存地址的编号。
public native int hashCode();
3、equals方法:判断对象是否是同一个对象
public boolean equals(Object obj) {
return (this == obj);
}
4、clone方法,复制对象
protected native Object clone() throws CloneNotSupportedException;
6、线程唤醒
public final native void notify();
public final native void notifyAll();
7、线程等待
public final native void wait(long timeout) throws InterruptedException;
8、finalize方法,垃圾回收时调用方法
protected void finalize() throws Throwable { }
9、toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public class ThreadF {
public static void main(String[] args) {
final Object object = new Object();
Thread t1 = new Thread() {
public void run()
{
synchronized (object) {
System.out.println("T1 start!");
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T1 end!");
}
}
};
Thread t2 = new Thread() {
public void run()
{
synchronized (object) {
System.out.println("T2 start!");
object.notify();
System.out.println("T2 end!");
}
}
};
Thread t3 = new Thread() {
public void run()
{
synchronized (object) {
System.out.println("T3 start!");
object.notify();
System.out.println("T3 end!");
}
}
};
Thread t4 = new Thread() {
public void run()
{
synchronized (object) {
System.out.println("T4 start!");
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T4 end!");
}
}
};
t1.start();
t2.start();
t3.start();
t4.start();
}
}