Java Object类

Object Structure

Java Object类_第1张图片
image.png

1,线程同步方法

1)notify()方法

  • 唤醒在当前对象监视器上等待的单个线程。Wakes up a single thread that is waiting on this object's monitor.
  • 必须是对象监听器的拥有者线程才能执行notify方法。This method should only be called by a thread that is the owner of this object's monitor.
  • 成为对象监听器拥有者的3中方法。A thread becomes the owner of the object's monitor in one of three ways
    执行该对象的synchronized实例方法By executing a synchronized instance method of that object.
    执行该对象的synchronized语句By executing the body of a synchronized statement that synchronizes on the object.
    执行类的synchronized静态方法executing a synchronized static method of that class.
    2)notifyAll()方法
    唤醒在当前对象监听器上等待的所有线程。Wakes up all threads that are waiting on this object's monitor.
    必须是对象监听器的拥有者线程才能执行notify方法。This method should only be called by a thread that is the owner of this object's monitor.
    3)wait方法
    导致当前线程等待,直到其他线程调用notify或者notifyAll方法。Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

2,其他方法

1)hashCode方法Returns a hash code value for the object.
对hash表结构提供支持。例如HashMap、hashTable等。This method is supported for the benefit of hash tables such as those provided by HashMap.
2)toString方法
默认return getClass().getName() + "@" + Integer.toHexString(hashCode());
3)equals方法
默认比较两个引用是否相同。return (this == obj);
4)public final native Class getClass();方法。
获取对象的类类型。

3,垃圾回收

1)finalize方法
GC在回收对象之前调用该方法。Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

你可能感兴趣的:(Java Object类)