Thread.sleep()方法使得线程主动放弃CPU执行权,在指定时间不参与竞争。休眠结束后,线程进入就绪状态,需重新抢占CPU才能执行,因此实际执行时间常大于设定时间,尤其是在CPU繁忙时误差更明显。
摘要生成于
,由 DeepSeek-R1 满血版支持,
Thread.sleep()方法的作用是使当前线程休眠一段时间,例如:Thread.sleep(1000)就是让线程休眠1000毫秒,也就是1秒,但实际情况是线程等待的时间总是大于1000毫秒的,下面有段测试代码:
public class Demo {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
for (int i=0;i<1000;i++){
Thread.sleep(1);
long end = System.currentTimeMillis();
System.out.println((end-start));
如果Thread.sleep()休眠时间到了之后线程是立即执行的,那最后打印出来的结果会是1000,但实际情况总是大于1000的,其原因是Thread.sleep()方法的作用是使线程主动放弃cpu的执行权,并在休眠的这段时间内不参与cpu的竞争,在休眠结束后,线程并不能马上得到执行,而是进入就绪状态,参与cpu抢占,只有得到cpu的执行权才能继续执行,所以线程的休眠时间总是大于设置的时间的,并且当cpu越忙的时候这个误差越大。
这几天在深入了解多
线程
,当学习到关于
线程
可见性的研究时,我写 demo 突然发现一个问题:
下面的代码是模拟
线程
可见性的。主
线程
(main)先启动,然后启动子
线程
(
Thread
VolatileDemo ),flag 初始值为 true,然后主
线程
将 flag 设置为 false。
由于
线程
的可见性,主
线程
的 flag = false 会存入主内存,然后子
线程
去读取主内存,while 循环结束。
可是!!!!情况有点不对!!!
public class
Thread
Volatile {
sleep
存在异常InterruptedException;
sleep
时间
达到后
线程
进入就绪状态
sleep
可以模拟网络延迟(就是在run方法中加一个
sleep
方法就可以了),倒计时等
每一个对象都有一个锁,
sleep
不会释放锁;
package com
.
Jinone
.
www;
import
java
.
text
.
SimpleDateFormat;
import
java
.
util
.
Date;
public class TextS
1
Thread
.
sleep
()
使当前执行的
线程
在指定的毫秒数内
休眠
(暂时停止执行),这取决于系统计时器和调度程序的精度和准确性,
线程
不会丢失任何监视器的所有权。
public class Test
Thread
Sleep
{
public static void main(String[] args) {
System
.
out
.
println("Main
thread
begin time = " + System
.
currentTimeMillis
()
);
文章目录前言
Thread
.
sleep
()
1
.
看看JDK中此方法上面的注释2
.
案例展示(
线程
状态:TIMED_WAITING)3
.
案例展示(InterruptedException异常会清除interrupted status)4
.
案例展示(
sleep
不会释放monitors锁)总结
如果让一个
线程
进入
休眠
?
我们第一个想到的就是
Thread
.
sleep
(long millis)方法吧!
如果你对synchronized足够了解的话,那么你肯定还能想到Object
.
wait
()
方法。
再如果你对jav
Thread
.
sleep
()
Thecurrent
thread
changes state fromRunningtoWaiting/Blocked as shown in the diagram below
.
Any other
thread
with reference to the
thread
currently
sleep
ing (say t) ca
.
.
.
第一种:public static void
sleep
(long millis)
throws InterruptedException使当前正在执行的
线程
以指定的毫秒数暂停(暂时停止执行),具体取决于系统定时器和调度程序的精度和准确性。
线程
不会丢失任何显示器的所有权。
millis - 以毫秒为单位的睡眠
时间
长度
Thread
.
sleep
(300)
//表示睡眠300毫秒
记得要处理异常
第二种:public static void
sleep
(long millis, int nano
1、静态方法:
Thread
.
sleep
(1000);
2、参数是毫秒
3、作用:让当前
线程
进入
休眠
,进入“阻塞状态”,放弃占有CPU
时间
片,让给其它
线程
使用。
这行代码出现在A
线程
中,A
线程
就会进入
休眠
。
这行代码出现在B
线程
中,B
线程
就会进入
休眠
。
4、
Thread
.
sleep
()
方法,可以做到这种效果:
间隔特定的
时间
,去
Thread
.
sleep
()
is a method in
Java
that causes the current
thread
to pause execution for a specific amount of time
.
It is used to introduce a delay or pause in the execution of a program
.
The syntax for
Thread
.
sleep
()
is:
```
java
try {
Thread
.
sleep
(milliseconds);
} catch (InterruptedException e) {
// Handle the exception
Here, the parameter milliseconds specifies the amount of time in milliseconds that the
thread
should
sleep
.
The catch block is used to handle the InterruptedException that may be thrown if the
thread
is interrupted while it is
sleep
ing
.
For example, if we want to pause the execution of a program for 1 second, we can use
Thread
.
sleep
(1000) like this:
```
java
try {
Thread
.
sleep
(1000); // Pause execution for 1 second
} catch (InterruptedException e) {
// Handle the exception
Note that
Thread
.
sleep
()
is a static method, so it can be called directly on the
Thread
class without creating an instance of the class
.