当前位置:网站首页>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 .
边栏推荐
- Fabric. JS document
- Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
- fpga阻塞赋值和非阻塞赋值
- JVM runtime data area
- FPGA test method takes mentor tool as an example
- Zabbix添加Calculated items后保存页面成空白
- Find specified characters
- Nucleic acid modified metal organic framework drug carrier | pcn-223 metal organic framework encapsulated ad adamantane | zif-8 encapsulated adriamycin (DOX)
- 7-7 12-24 hour system
- Common plug-ins for vite project development
猜你喜欢

JS matrix zero

常见问题之PHP——ldap_add(): Add: Undefined attribute type in

Leetcode(4)——尋找兩個正序數組的中比特數

天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪

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

使用并行可微模拟加速策略学习

Current situation, analysis and prediction of information and innovation industry

protobuf与grpc

7-11 calculation of residential water charges by sections

Vite project commissioning
随机推荐
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
Thread.sleep和TimeUnit.SECONDS.sleep的区别
Exercise 10-8 recursive implementation of sequential output of integers
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
js 2023. String pair equal to the target string after connection
Leetcode(4)——尋找兩個正序數組的中比特數
Although not necessarily the best, it must be the hardest!
QT learning 17 dialog box and its types
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
超简单手机地图开发
Statistical capital consonants
JS matrix zero
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
Eight sorts
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
Redis:Redis的数据结构、key的操作命令
Jiuyi cloud black free encryption free version source code
Page generation QR code