当前位置:网站首页>Thread. Sleep and timeunit SECONDS. The difference between sleep
Thread. Sleep and timeunit SECONDS. The difference between sleep
2022-07-03 14:21:00 【There is neither wind nor rain tonight】
Catalog
effect
When the thread needs to sleep for a period of time , have access to Thread.sleep/TimeUnit.SECONDS.sleep Make the current thread pause execution , Both can be achieved , No difference . Wait until the sleep time ends , The thread can continue to execute .
difference
Thread.sleep() yes java.lang Class method under package , The received parameter unit is milliseconds , If you need to pause the current thread for seconds , minute , Hours, etc , It needs to be converted manually according to the millisecond unit ;TimeUnit.SECONDS.sleep() yes java.util.concurrent Class method under package , The bottom call is still Thread.sleep(), It's right Thread.sleep() Packaging of methods , More time unit conversion and verification .
public void sleep(long timeout) throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
int ns = excessNanos(timeout, ms);
Thread.sleep(ms, ns);
}
}
When TimeUnit It's followed by SECONDS, be TimeUnit.SECONDS.sleep(long time) Means to pause the thread time Second , The same can be TimeUnit.MINUTES.sleep(); TimeUnit.HOURS.sleep(); TimeUnit.DAYS.sleep();
expand
Thread.sleep() You can make the current thread sleep for a period of time , But the lock held in this thread will not be released , Just temporarily give up the right of execution of time , Do not participate during cpu The scheduling . When sleep time is up , The thread will be in a ready state , To be obtained cpu After the scheduling time slice , Will continue to run .
about wait() Method , Will release the synchronization resource lock , It is convenient for the thread waiting for the resource to get the resource and then run , Only other threads call notify()/notifyall() After method , The thread will release the waiting state , So as to participate in the competition to synchronize resource locks .( Be careful :wait() yes Object Methods in class )
If you want a thread A The logic inside needs to be in the thread B Execute after the logic execution in , have access to Thread Class join(long time) Method , It can be set flexibly time value .
边栏推荐
- Exercise 10-2 recursive factorial sum
- Redis:字符串類型數據的操作命令
- Understanding of closures
- Redis: commandes d'action pour les données de type chaîne
- 7-19 check denomination (solve binary linear equation)
- Programmable logic device software testing
- 7-16 find the set of integers that meet the given conditions
- 一文了解微分段应用场景与实现机制
- QT learning 20 standard dialog box in QT (middle)
- [Jilin University] information sharing of postgraduate entrance examination and re examination
猜你喜欢
7-7 12-24 hour system
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
编程语言:类型系统的本质
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
Exercise 6-1 classify and count the number of characters
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
7-11 calculation of residential water charges by sections
Global event bus
Exercise 9-3 plane vector addition
随机推荐
JS get DPI, PX to cm, cm to PX
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
JVM class loading
7-14 sum integer segments (C language)
JVM垃圾回收机
7-20 print 99 formula table (format output)
Sendmail无法发送邮件及发送过慢解决
Exercise 10-3 recursive implementation of exponential functions
How to delete an attribute or method of an object
Invalid Z-index problem
7-8 overspeed judgment
战略、战术(和 OKR)
Understanding of closures
Too many files with unapproved license
C language,%d% Difference between 2D%2d%02d
JVM garbage collector
Fabric. JS document
7-4 BCD decryption (10 points)
Nucleic acid modified metal organic framework drug carrier | pcn-223 metal organic framework encapsulated ad adamantane | zif-8 encapsulated adriamycin (DOX)
Leetcode(4)——寻找两个正序数组的中位数