Java基础-Object

概述

  • 顶级父类

常规方法

private static native void registerNatives();

static {
        registerNatives();
}
  • 对象运行时类类型

public final native Class<?> getClass();
  • 非覆盖情况下,返回对象地址;覆盖时与equals保持一致

public native int hashCode();
  • 非覆盖情况下比较JVM地址值

public boolean equals(Object obj) {
        return (this == obj);
}
  • 约定:equals的对象,hashCode必等;反之实现为佳,但hash算法不佳时,难免碰撞

  • 拷贝生成对象副本,常规约定下

    • x.clone() != x
    • x.clone().getClass() == x.getClass()
    • x.clone().equals(x)
  • 涉及深拷贝时最后一条不一定满足

  • 必须实现Cloneable接口方可调用clone方法,否则抛CloneNotSupportedException

protected native Object clone() throws CloneNotSupportedException;
  • 返回对象的描述信息

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

锁相关方法

  • 唤醒

// 随机唤醒
public final native void notify();

// 唤醒全部
public final native void notifyAll();
  • 休眠

// 释放锁,休眠
public final native void wait(long timeout) throws InterruptedException;
  • 唤醒方式
    • notify + 高RP
    • notifyAll
    • 其他线程中断该线程,先重置中断位状态,再抛InterruptedException
    • 等待时间耗光;时间设为0,代表一直等待
  • spurious wakeup假唤醒
    • 为避免这种极少出现但可能出现的情况对应用的影响,需要将wait放在循环里,即wait条件还达到的话,就不断执行wait

synchronized (obj) {
    while (<condition does not hold>;)
        obj.wait(timeout);
    // Perform action appropriate to condition
}

gc相关

  • gc时会用一个低优先级线程去调,只会调用一次
  • 会保证被调用,但无法保证执行完毕,如果卡死什么的,会直接被JVM中断
  • 初期作为C++析构函数的一种折中,其实并不建议使用

protected void finalize() throws Throwable { }
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eddy咸鱼

感谢大佬加鸡蛋~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值