DXP BLOG
首页
  • JDk
  • Spring系列
  • 微服务
  • Json
  • Netty
  • Bug
  • Mysql
  • Postgresql
  • 达梦
  • activemq
  • rabbitmq
  • rocketmq
  • redis
  • Vue
  • React
  • Angular
  • Javascript
  • Typescript
linux
  • 协议
  • 加解密
  • 分类
  • 标签
  • 归档
Gitee (opens new window)

董新平

一个普普通通的代码程序猿
首页
  • JDk
  • Spring系列
  • 微服务
  • Json
  • Netty
  • Bug
  • Mysql
  • Postgresql
  • 达梦
  • activemq
  • rabbitmq
  • rocketmq
  • redis
  • Vue
  • React
  • Angular
  • Javascript
  • Typescript
linux
  • 协议
  • 加解密
  • 分类
  • 标签
  • 归档
Gitee (opens new window)
  • JDK

    • ThreadPoolExecutor
    • Object类12中方法及作用
      • 1. registerNatives()
      • 2. getClass()
      • 3. hashCode
      • 4.equals()
      • 5. clone()
      • 6. toString()
      • 7 wait()
      • 8 wait(long)
      • 9 wait(long timeout, int nanos)
      • 10 notify()
      • 11 notifyAll()
      • 12 finalize()
    • CountDownLatch
    • 读写锁
    • Atomic
  • Spring系列

  • Json

  • JAVA
  • JDK
dongxinping
2022-08-23
目录

Object类12中方法及作用

# Object 类 12 中方法及作用

  • registerNatives()
  • getClass()
  • hashCode()
  • equals()
  • clone()
  • toString()
  • wait()
  • wait(long)
  • wait(long l, int i)
  • notify()
  • notifyAll()
  • finalize()

# 1. registerNatives()

private static native void registerNatives();
static {
   registerNatives();
}
1
2
3
4

注册本地方法, JVM 底层是 C/C++实现.本地方法的实现是由其他语言编写并保存在动态连接库中,因而在 java 类中不需要方法实现

# 2. getClass()

public final native Class<?> getClass();
1

final 方法、获取对象的运行时 class 对象,class 对象就是描述对象所属类的对象。这个方法通常是和 Java 反射机制搭配使用的.

# 3. hashCode

public native int hashCode();
1

该方法主要用于获取对象的散列值。Object 中该方法默认返回的是对象的堆内存地址。如果我们要用自定义类对象做散列表的 key 时,在重写了 equals()后,还要重写 hashCode()来保证 equals()返回为 true 的情况下两个对象 hashCode()的返回值也相等。

# 4.equals()

public boolean equals(Object obj) {        return (this == obj);}
1

该方法用于比较两个对象,如果这两个对象引用指向的是同一个对象,那么返回 true,否则返回 false。一般 equals 和 == 是不一样的,但是在 Object 中两者是一样的。子类一般都要重写这个方法。String 类已经重写了equals方法, 所以 string 的==和 equals 方法不一样.

# 5. clone()

protected native Object clone() throws CloneNotSupportedException;
1

实现了 Cloneable 接口后可以调用这个方法,否则抛出 CloneNotSupportedException 异常. 默认的 clone 方法实现的浅拷贝,只会拷贝引用地址而不会将引用的对象重新分配内存.

# 6. toString()

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

返回一个 String 对象,一般子类都有覆盖。

# 7 wait()

public final void wait() throws InterruptedException {
    wait(0);
}
1
2
3

# 8 wait(long)

public final native void wait(long timeout) throws InterruptedException;
1

wait 方法就是使当前线程等待该对象的锁,当前线程必须是该对象的拥有者,也就是具有该对象的锁。wait() 方法一直等待,直到获得锁或者被中断。wait(long timeout) 设定一个超时间隔,如果在规定时间内没有获得锁就返回。timeout=0 表示一直等待

# 9 wait(long timeout, int nanos)

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 > 0) {
        timeout++;
    }

    wait(timeout);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 10 notify()

public final native void notify();
1

主要用于随机唤醒在该对象上等待的某个线程,唤醒的顺序需要根据具体的 JVM 实现有所不同. 主流的 hotspot 中,其内部有一个 _WaitSet 去维护 wait() 线程的顺序.

# 11 notifyAll()

public final native void notifyAll();
1

唤醒在该对象上等待的所有线程

# 12 finalize()

protected void finalize() throws Throwable { }
1

用于在 GC 的时候再次被调用. 垃圾回收器将要回收对象所占内存之前被调用,即当一个对象被虚拟机宣告死亡时会先调用它 finalize()方法,让此对象处理它生前的最后事情(这个对象可以趁这个时机挣脱死亡的命运,当前对象的引用 this 赋值给某对象的类变量/成员变量,重新建立可达的引用).

用来做最后的资源释放和清理

#源码阅读
上次更新: 2023/12/29, 17:39:06
ThreadPoolExecutor
CountDownLatch

← ThreadPoolExecutor CountDownLatch→

最近更新
01
Redis数据类型
01-20
02
Atomic
12-27
03
编译安装Redis
12-27
更多文章>
dongxinping | Copyright © 2022-2024 Dongxinping | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式