当前位置:网站首页>Thread.sleep和TimeUnit.SECONDS.sleep的区别
Thread.sleep和TimeUnit.SECONDS.sleep的区别
2022-07-03 14:02:00 【今夜无风亦无雨】
目录
作用
当需要将线程休眠一段时间的时候,可以使用Thread.sleep/TimeUnit.SECONDS.sleep的方式让当前线程暂停执行,二者都可以实现,并无差异。待休眠时间结束,线程可以继续执行。
区别
Thread.sleep()是java.lang包下的类方法,接收的参数单位是毫秒,若需要让当前线程暂停执行秒,分钟,小时等,需要手动根据毫秒单位来换算;TimeUnit.SECONDS.sleep()是java.util.concurrent包下的类方法,底层调用的依旧是 Thread.sleep(),是对 Thread.sleep()方法的包装,多了时间单位转换和验证。
public void sleep(long timeout) throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
int ns = excessNanos(timeout, ms);
Thread.sleep(ms, ns);
}
}
当TimeUnit后面跟的是SECONDS,则TimeUnit.SECONDS.sleep(long time)表示的让线程暂停time秒钟,同理可得TimeUnit.MINUTES.sleep(); TimeUnit.HOURS.sleep(); TimeUnit.DAYS.sleep();
拓展
Thread.sleep()可以让当前线程休眠一段时间,但是在该线程不会释放持有的锁,只是暂时让出时间的执行权,期间不参与cpu的调度。休眠时间到了之后,线程会处于就绪状态,待获得cpu的调度时间片之后,会继续运行。
对于wait()方法,则会释放同步资源锁,便于等待该资源的线程得到该资源进而运行,只有其他线程调用notify()/notifyall()方法之后,该线程才会解除等待状态,从而去参与竞争同步资源锁。(注意:wait()是Object类中的方法)
若希望一个线程A内的逻辑需要在线程B内的逻辑执行结束之后执行,可以使用Thread类的join(long time)方法,其中可以灵活设置time值。
边栏推荐
- Exercise 6-6 use a function to output an integer in reverse order
- 关于回溯问题中的排列问题的思考(LeetCode46题与47题)
- 八大排序
- 消息订阅与发布
- etcd集群权限管理和账号密码使用
- 战略、战术(和 OKR)
- [Jilin University] information sharing of postgraduate entrance examination and re examination
- Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
- Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
- 7-7 12-24 hour system
猜你喜欢
QT learning 20 standard dialog box in QT (middle)
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
Fabric. JS document
编程语言:类型系统的本质
Redis: operation command of string type data
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
JS first summary
npm install卡住与node-npy的各种奇怪报错
QT learning 23 layout manager (II)
protobuf与grpc
随机推荐
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
Understanding of closures
信创产业现状、分析与预测
QT learning 21 standard dialog box in QT (Part 2)
1px problem of mobile terminal
JS download files through URL links
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
Exercise 7-6 count capital consonants
JS continues to explore...
Configure stylelint
[clean up the extraordinary image of Disk C]
Duet date picker (time plug-in that can manually enter the date)
fpga阻塞赋值和非阻塞赋值
Facebook 如何将 Instagram 从 AWS 搬到自己的服务器
Print. JS -- web page file printing
Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
js . Find the first palindrome string in the array
JS Part III
Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
7-9 find a small ball with a balance