当前位置:网站首页>wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
2022-06-25 10:58:00 【用户9854323】
wait()、notify()和notifyAll()是 Object类 中的方法 从这三个方法的文字描述可以知道以下几点信息: 1) wait()、notify()和notifyAll()方法是本地方法,并且为final方法,无法被重写。 2)调用某个对象的wait()方法能让当前线程阻塞,并且当前线程必须拥有此对象的monitor(即锁) 3) 调用某个对象的notify()方法能够唤醒一个正在等待这个对象的monitor的线程,如果有多个线程都在等待这个对象的monitor,则只能唤醒其中一个线程; 4) 调用notifyAll()方法能够唤醒所有正在等待这个对象的monitor的线程; 5 ) 如果调用某个对象的wait()方法,当前线程必须拥有这个对象的monitor(即锁),因此调用wait()方法必须在同步块或者同步方法中进行(synchronized块或者synchronized方法)。 6 ) 调用某个对象的wait()方法,相当于让当前线程交出此对象的monitor,然后进入等待状态,等待后续再次获得此对象的锁(Thread类中的sleep方法使当前线程暂停执行一段时间,从而让其他线程有机会继续执行,但它并不释放对象锁); 7 ) 调用某个对象的notify()方法,当前线程也必须拥有这个对象的monitor,因此调用notify()方法必须在同步块或者同步方法中进行(synchronized块或者synchronized方法)。
Condition是在java 1.5中才出现的,它用来替代传统的Object的wait()、notify()实现线程间的协作,相比使用Object的wait()、notify(),使用Condition的await()、signal()这种方式实现线程间协作更加安全和高效。 因此通常来说比较推荐使用Condition。
1 ) Condition是个接口,基本的方法就是await()和signal()方法; 2 ) Condition依赖于Lock接口,生成一个Condition的基本代码是lock.newCondition() 3 ) 调用Condition的await()和signal()方法,都必须在lock保护之内,就是说必须在lock.lock()和lock.unlock之间才可以使用
Conditon中的await()对应Object的wait(); Condition中的signal()对应Object的notify(); Condition中的signalAll()对应Object的notifyAll()
边栏推荐
- Previous string inversion topic
- [image fusion] image fusion based on morphological analysis and sparse representation with matlab code
- Daily 3 questions (3) - check whether integers and their multiples exist
- Continuous delivery jenkinsfile syntax
- 过拟合原因及解决
- Nuxtjs actual combat case
- Android: generic mapping analysis of gson and JSON in kotlin
- 金仓数据库 KingbaseES 插件DBMS_OUTPUT
- 今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
- ARM64特有一些的汇编指令
猜你喜欢
【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
Advanced single chip microcomputer -- development of PCB (2)
scrapy+scrapyd+gerapy 爬虫调度框架
开源社邀请您参加OpenSSF开源安全线上研讨会
Coscon'22 lecturer solicitation order
Nuxtjs actual combat case
A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students
Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)
ES 学习
随机推荐
Netease's open source distributed storage system curve officially became the CNCF sandbox project
Is it safe for Guosen Securities to open a securities account
Free access to the global human settlements layer (ghsl) dataset from Gee
MySQL synchronous data configuration and shell script implementation
Upload and modify the use of avatars
COSCon'22 讲师征集令
Daily Mathematics Series 52: February 20
ZABBIX distributed system monitoring
FPGA displays characters and pictures based on VGA
Google Earth Engine(GEE)——evaluate实现一键批量下载研究区内的所有单张影像(上海市部分区域)
Technical practice and development trend of video conference all in one machine
Simple use of SVN
[维护集群案例集] GaussDB 查询用户空间使用情况
Think about it
从GEE中免费获取全球人类住区层 (GHSL) 数据集
Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
16 enterprise architecture strategies
Network remote access using raspberry pie
Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
Server rendering