Java多线程系列(六)wait,notify 与notifyall

本文深入探讨了Java中线程同步的三大核心方法:wait、notify和notifyAll。wait方法使当前线程进入等待状态,直到被notify或notifyAll唤醒。notify方法随机唤醒一个处于等待状态的线程,而notifyAll则唤醒所有等待的线程,使它们有机会重新竞争对象锁。

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

1.wait方法

根据注释:

it places the current thread
* into the wait set for this object

进入wait set

 

2.notify方法

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. A thread waits on an object's
* monitor by calling one of the {@code wait} methods.

从wait set中取出一个线程,可以重新竞争锁

 

3.notifyall方法

唤醒所有wait set中的线程

wakes up all threads that are waiting on this object's monitor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值