线程间通信之Object.wait/notify实现

本文详细解析Java中线程通信的关键概念,包括synchronized、wait和notify的使用原理,探讨了monitor的作用以及wait和notify方法的具体实现。通过具体代码示例,分析了线程在获取锁、等待和唤醒过程中的行为,揭示了HotSpot虚拟机中ObjectMonitor的内部机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

核心有这样几个关键字:锁,monitor以及指令。PS:遇到面试官问的问题不是你曾经考虑的问题不要怕,不要紧张。可能只是说法变了,但是知识还是那些知识!

【1】线程通信实例分析

先看一个线程通信例子:

public class WaitNotifyCase {
    public static void main(String[] args) {
        final Object lock = new Object();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread A is waiting to get lock");
                synchronized (lock) {
                    try {
                        System.out.println("thread A get lock");
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println("thread A do wait method");
                        lock.wait();
                        System.out.println("wait end");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread B is waiting to get lock");
                synchronized (lock) {
                    System.out.println("thread B get lock");
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.notify();
                    System.out.println("thread B do notify method");
                }
            }
        }).start();
    }
}

很常见的synchronized、wait和notify。由同一个lock对象调用wait、notify方法。当线程A执行wait方法时,该线程会被挂起;当线程B执行notify方法时,会唤醒一个被挂起的线程A。

两个疑问

1、进入wait/notify方法之前,为什么要获取synchronized锁?
2、线程A获取了synchronized锁,执行wait方法并挂起,线程B又如何再次获取锁?

为什么使用synchronized

如下代码所示:

static void Sort(int [] array) {
    // synchronize this operation so that some other thread can't
    // manipulate the array while we are sorting it. This assumes that other
    // threads also synchronize their accesses to the array.
    synchronized(array) {
        // now sort elements in array
    }
}

查看其字节码:
在这里插入图片描述
synchronized代码块通过javap生成的字节码中包含 monitorentermonitorexit指令。

执行monitorenter指令可以获取对象的monitor,而lock.wait()方法通过调用native方法wait(0)实现,其中接口注释中有这么一句:

The current thread must own this object's monitor.

表示线程执行lock.wait()方法时,必须持有该lock对象的monitor,如果wait方法在synchronized代码中执行,该线程很显然已经持有了monitor。


代码执行过程分析

1、在多核环境下,线程A和B有可能同时执行monitorenter指令,并获取lock对象关联的monitor,只有一个线程可以和monitor建立关联,假设线程A执行加锁成功;
2、线程B竞争加锁失败,进入等待队列进行等待;
3、线程A继续执行,当执行到wait方法时,会发生什么?wait接口注释:

This method causes the current thread to place itself in the wait set for this object 
and then to relinquish any and all synchronization claims on this object.

wait方法会将当前线程放入wait set,等待被唤醒,并放弃lock对象上的所有同步声明。意味着线程A释放了锁,线程B可以重新执行加锁操作。不过又有一个疑问:在线程A的wait方法释放锁,到线程B获取锁,这期间发生了什么?线程B是如何知道线程A已经释放了锁

4、线程B执行加锁操作成功,对于notify方法,JDK注释:notify方法会选择wait set中任意一个线程进行唤醒;

Wakes up a single thread that is waiting on this object's monitor. If any threads
are waiting on this object, one of them is chosen to be awakened. The choice is 
arbitrary and occurs at the discretion of the implementation

notifyAll方法的注释:notifyAll方法会唤醒monitor的wait set中所有线程。

Wakes up all threads that are waiting on this object's monitor.

5、执行完notify方法,并不会立马唤醒等待线程,在notify方法后面加一段sleep代码就可以看到效果,如果线程B执行完notify方法之后sleep 5s,在这段时间内,线程B依旧持有monitor,线程A只能继续等待。

那么wait set的线程什么时候会被唤醒?想要解答这些疑问, 需要分析jvm的相关实现,本文以HotSpot虚拟机1.7版本为例。


【2】什么是monitor?

在HotSpot虚拟机中,monitor采用ObjectMonitor实现。
在这里插入图片描述
每个线程都有两个ObjectMonitor对象列表,分别为free和used列表,如果当前free列表为空,线程将向全局global list请求分配ObjectMonitor

ObjectMonitor对象中有两个队列:_WaitSet_EntryList,用来保存ObjectWaiter对象列表;_owner指向获得ObjectMonitor对象的线程。

在这里插入图片描述
_WaitSet :处于wait状态的线程,会被加入到wait set;
_EntryList:处于等待锁block状态的线程,会被加入到entry set;


【3】ObjectWaiter

在这里插入图片描述
ObjectWaiter对象是双向链表结构,保存了_thread(当前线程)以及当前的状态TState等数据, 每个等待锁的线程都会被封装成ObjectWaiter对象


【4】wait方法实现原理

lock.wait()方法最终通过ObjectMonitor的void wait(jlong millis, bool interruptable, TRAPS);实现。

1、将当前线程封装成ObjectWaiter对象node;
在这里插入图片描述
2、通过ObjectMonitor::AddWaiter方法将node添加到_WaitSet列表中;
在这里插入图片描述
3、通过ObjectMonitor::exit方法释放当前的ObjectMonitor对象,这样其它竞争线程就可以获取该ObjectMonitor对象。
在这里插入图片描述

4、最终底层的park方法会挂起线程。


【5】notify方法实现原理

lock.notify()方法最终通过ObjectMonitor的void notify(TRAPS)实现。

1、如果当前_WaitSet为空,即没有正在等待的线程,则直接返回;
2、通过ObjectMonitor::DequeueWaiter方法,获取_WaitSet列表中的第一个ObjectWaiter节点,实现也很简单。(这里需要注意的是,在jdk的notify方法注释是随机唤醒一个线程,其实是第一个ObjectWaiter节点。)
在这里插入图片描述
3、根据不同的策略,将取出来的ObjectWaiter节点,加入到_EntryList或则通过Atomic::cmpxchg_ptr指令进行自旋操作cxq。


【6】notifyAll方法实现

lock.notifyAll()方法最终通过ObjectMonitor的void notifyAll(TRAPS)实现。
通过for循环取出_WaitSet的ObjectWaiter节点,并根据不同策略,加入到_EntryList或则进行自旋操作。

从JVM的方法实现中,可以发现:notify和notifyAll并不会释放所占有的ObjectMonitor对象(wait方法会释放Objectmonitor对象),其实真正释放ObjectMonitor对象的时间点是在执行monitorexit指令,一旦释放ObjectMonitor对象了,entry set中ObjectWaiter节点所保存的线程就可以开始竞争ObjectMonitor对象进行加锁操作了。

参考博文:
一文读懂Synchronized的实现原理
Thread入门与线程方法详解及多线程安全
JVM源码分析之Object.wait/notify实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

流烟默

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值