当前位置:网站首页>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 .
边栏推荐
- 中国锂电池电解液行业市场专项调研报告(2022版)
- 7-23 currency conversion (using array conversion)
- 一文了解微分段应用场景与实现机制
- Exercise 8-8 moving letters
- Find specified characters
- 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.)
- Programmable logic device software testing
- JS Part 2
- Strategy, tactics (and OKR)
- 7-24 reduction of the simplest fraction (rolling Division)
猜你喜欢
Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
Exercise 6-6 use a function to output an integer in reverse order
protobuf与grpc
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
Global event bus
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
愉悦资本新双币基金近40亿元完成首次关账
Exercise 9-1 time conversion
7-15 calculation of PI
Exercise 10-8 recursive implementation of sequential output of integers
随机推荐
Find specified characters
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Leetcode(4)——寻找两个正序数组的中位数
Redis:字符串類型數據的操作命令
Redis:Redis的数据结构、key的操作命令
Solr series of full-text search engines - basic principles of full-text search
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
7-7 12-24 hour system
JVM object lifecycle
How to bold text in AI
7-9 find a small ball with a balance
Exercise 10-2 recursive factorial sum
Configure stylelint
Convert string to decimal integer
Print. JS -- web page file printing
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
Raft 协议
JVM garbage collector
7-4 BCD decryption (10 points)
Find the sum of the elements of each row of the matrix