源码:java.lang.Object 源码解析

1. TODO


2. 脑图

  1. Xmind

  2. Edraw

  3. Hexo 地址
    👉 http://blog.wangjia.ink/2025/09/24/源码:java.lang.Object源码解析/


3. 基础部分

3.1. Object 概述

Object 是一个具体类


4. 源码部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public class Object {

@IntrinsicCandidate
public Object() {}

// 用于非阻塞获取类对象
@IntrinsicCandidate
public final native Class<?> getClass();

// 用于非阻塞获取哈希码
@IntrinsicCandidate
public native int hashCode();

// 用于非阻塞比较是否 ”相等“
//
// 默认比较的是内存地址
public boolean equals(Object obj) {
return (this == obj);
}

// 用于配置克隆机制,详见笔记:GOF 设计模式(obsidian 内部链接:[笔记:GOF设计模式](笔记:GOF设计模式.md),Hexo 链接:)
@IntrinsicCandidate
protected native Object clone() throws CloneNotSupportedException;

// 用于非阻塞获取 ”字符串表示形式“
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

// 用于非阻塞唤醒 Monitor 的 WaitSet 中的 “任意” 一个 Thread 实例,被唤醒的 Thread 实例被直接投递到 Monitor 的 EntryList 队列
//
// 所谓的 “任意” 是指:既不是严格的随机,也不保证是第一个,不同的 JVM 实现可能采用不同的策略
//
// Object#notify 是和 synchronized 绑定的,它必须出现在 synchronized 代码块中,否则会抛出异常。也就是说:本地线程要想调用 Object#notify,必须先获取 Monitor 锁。
@IntrinsicCandidate
public final native void notify();

// 用于非阻塞唤醒 Monitor 的 WaitSet 中的所有 Thread 实例,被唤醒的 Thread 实例被直接投递到 Monitor 的 EntryList 队列
//
// Object#notify 是和 synchronized 绑定的,它必须出现在 synchronized 代码块中,否则会抛出异常。也就是说:本地线程要想调用 Object#notify,必须先获取 Monitor 锁。
@IntrinsicCandidate
public final native void notifyAll();

// 用于让本地线程进入阻塞状态,Thread 实例进入 WAITING 状态,并被投递到 Monitor 的 WaitSet 队列,等待被唤醒(Object#notify)、被中断
//
// 本地线程进入阻塞状态,Thread 实例进入 WAITING 状态后,能响应中断。线程被唤醒,重新获得 CPU 时间片后,会抛出 InterruptedException 受检异常,并清除 Thread 实例的中断状态(异常退出或正常退出(发生异常),要看我们是否对该异常进行捕获并处理)
//
// Object#wait 是和 synchronized 绑定的,它必须出现在 synchronized 代码块中,否则会抛出异常。也就是说:本地线程要想调用 Object#wait,必须先获取 Monitor 锁。从 Object#wait 唤醒的本地线程,同样也需要先获取 Monitor 锁才能继续向下执行。从 Object#wait 响应中断的本地线程,同样也需要先获取 Monitor 锁才能抛出InterruptedException 异常。
public final void wait() throws InterruptedException {
wait(0L);
}

// 用于让本地线程进入阻塞状态,Thread 实例进入 TIMED_WAITING 状态,并被投递到 Monitor 的 WaitSet 队列,等待被唤醒(Object#notify)、被中断
//
// 本地线程进入阻塞状态,Thread 实例进入 TIMED_WAITING 状态后,能响应中断。线程被唤醒,重新获得 CPU 时间片后,会抛出 InterruptedException 受检异常,并清除 Thread 实例的中断状态(异常退出或正常退出(发生异常),要看我们是否对该异常进行捕获并处理)
//
//
// Object#wait 是和 synchronized 绑定的,它必须出现在 synchronized 代码块中,否则会抛出异常。也就是说:本地线程要想调用 Object#wait,必须先获取 Monitor 锁。从 Object#wait 唤醒的本地线程,同样也需要先获取 Monitor 锁才能继续向下执行。从 Object#wait 响应中断的本地线程,同样也需要先获取 Monitor 锁才能抛出InterruptedException 异常。
//
// 推荐使用 TimeUnit#timedWait 替代该方法,因为其可读性更好:TimeUnit.SECONDES.timedWait(obj, 5)
public final native void wait(long timeoutMillis) throws InterruptedException;

// 用于让本地线程进入阻塞状态,Thread 实例进入 TIMED_WAITING 状态,并被投递到 Monitor 的 WaitSet 队列,等待被唤醒(Object#notify)、被中断
//
// 本地线程进入阻塞状态,Thread 实例进入 TIMED_WAITING 状态后,能响应中断。线程被唤醒,重新获得 CPU 时间片后,会抛出 InterruptedException 受检异常,并清除 Thread 实例的中断状态(异常退出或正常退出(发生异常),要看我们是否对该异常进行捕获并处理)
//
//
// Object#wait 是和 synchronized 绑定的,它必须出现在 synchronized 代码块中,否则会抛出异常。也就是说:本地线程要想调用 Object#wait,必须先获取 Monitor 锁。从 Object#wait 唤醒的本地线程,同样也需要先获取 Monitor 锁才能继续向下执行。从 Object#wait 响应中断的本地线程,同样也需要先获取 Monitor 锁才能抛出InterruptedException 异常。
//
// 推荐使用 TimeUnit#timedWait 替代该方法,因为其可读性更好:TimeUnit.SECONDES.timedWait(obj, 5)
public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
if (timeoutMillis < 0) {
throw new IllegalArgumentException("timeoutMillis value is negative");
}

if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}

if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
timeoutMillis++;
}

wait(timeoutMillis);
}

// 用于配置实例的 finalization 机制,详见笔记:JVM
@Deprecated(since="9")
protected void finalize() throws Throwable { }

}


源码:java.lang.Object 源码解析
https://wangjia5289.github.io/2025/09/24/源码:java.lang.Object源码解析/
Author
咸阳猴🐒
Posted on
September 24, 2025
Licensed under