zoukankan
html css js c++ java
sleep和wait区别
1. sleep和wait都是用来进行线程控制,他们最大本质的区别是:
sleep()不释放同步锁,wait()释放同步锁.
sleep(milliseconds)可以用时间指定来使他自动醒过来,如果时间不到你只能调用interreput()来强行打断;
wait()可以用notify()直接唤起.
2. sleep是Thread类的静态方法。
sleep的作用是让线程休眠制定的时间,在时间到达时恢复,也就是说sleep将在接到时间到达事件事恢复线程 执行,例如:
try{
System.out.println("I'm going to bed");
Thread.sleep(1000);
System.out.println("I wake up");
}
catch(IntrruptedException e) {
}
wait是Object的方法,也就是说可以对任意一个对象调用wait方法,调用wait方法将会将调用者的线程挂起,直到 其他线程调用同一个对象的notify方法才会重新激活调用者,例如:
try{
obj.wait
}
catch(InterrputedException e) {
}
3. sleep()是让某个线程暂停运行一段时间,其控制范围是由当前线程决定,
wait()是由某个确定的对象来调用的。
sleep和wait的区别有:
1,这两个方法来自不同的类分别是Thread和Object
2,最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。
3,wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在
任何地方使用
synchronized(x){
x.notify()
//或者wait()
}
4,sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常
查看全文
相关阅读:
用GDB调试程序(一)
Linux编程基础——GDB(设置断点)
滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(10月12日~10月18日)
北京Uber优步司机奖励政策(10月19日~10月25日)
借贷宝人脸识别无需绑卡,需合作者共同冲刺
王璐首次详解借贷宝诞生历程 直面创业从0到1
创造信用收入 借贷宝颠覆创新普惠金融
php中,如何将编译后的代码,反编译回去。
chrome调试ajax
提示text还能输入多少字节
原文地址:https://www.cnblogs.com/needly/p/3505139.html
最新文章
怎样计算任一天是星期几
根据日期推算星期和历法由来
闰年的计算方法
Ritchie Lawrence 批处理函数库中英文版
8 个最好的 jQuery 树形 Tree 插件
推荐10款优秀的JavaScript Web UI库 框架和套件
word如何让单页变横向
leetcode 657. Judge Route Circle
leetcode 461. Hamming Distance
leetcode 771. Jewels and Stones
热门文章
循环遍历Java字符串字符的规范方法——类似python for ch in string
包含utf8字符的 pickle 转 json的大坑处理过程
python pickle to json
mac os lscpu 【转】
查看tensorflow pb模型文件的节点信息
bazel编译tensorflow 生成libtensorflow_inference.so 和 libandroid_tensorflow_inference_java.jar
tensorflow 模型压缩
滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(10月17日~10月23日)
北京Uber优步司机奖励政策(10月26日~11月1日)
免费好用的Diff和Merge工具大总结
Copyright © 2011-2022 走看看