sleep、yield方法和线程优先级
sleep方法
- 调用 sleep 会让当前线程从 Running 进入 Timed Waiting 状态(阻塞);
- 其它线程可以使用 interrupt 方法打断正在睡眠的线程,这时 sleep 方法会抛出 InterruptedException;
- 睡眠结束后的线程未必会立刻得到执行,还得看任务调度器的心情;
- 建议用 TimeUnit 的 sleep 代替 Thread 的 sleep 来获得更好的可读性;
我们下面会用三个例子来说明上面的几点。
例一:
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class Test1 {
public static void main(String[] args) {
Thread thread = new Thread("thread"){
@Override
public void run() {
try {
//调用 sleep 会让当前线程从 Running 进入 Timed Waiting 状态(阻塞)
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
log.debug("thread state:{}",thread.getState());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("thread state:{}",thread.getState());
}
}
运行结果:
我们可以看到,在调用 sleep 方法后会让当前线程从 Running 进入 Timed Waiting 状态(阻塞)。
例二:
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class Test2 {
public static void main(String[] args) {
Thread thread = new Thread("thread") {
@Override
public void run() {
log.debug("进入睡眠");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
log.debug("thread线程醒过来了");
e.printStackTrace();
}
}
};
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("唤醒thread线程");
//其它线程可以使用 interrupt 方法打断正在睡眠的线程,这时 sleep 方法会抛出 InterruptedException
thread.interrupt();
}
}
运行结果:
我们在例二的代码中调用了interrupt方法来打断了此时正在睡眠的线程thread,因为被打断睡眠了,所以此时sleep抛出InterruptedException异常。
例三:
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
@Slf4j(topic = "c.Test3")
public class Test3 {
public static void main(String[] args) throws InterruptedException {
log.debug("开始睡眠");
//其实TimeUnit内部也是调用原生的sleep方法,
//只不过TimeUnit给它进行了一下单位的换算,增加了可读性(不用数那么多个0了...)
TimeUnit.SECONDS.sleep(1);
log.debug("结束睡眠");
}
}
运行结果:
其实TimeUnit最大的优点就是增加了可读性,再也不用数0了😂。而且TimeUnit的底层还是使用了Thread.sleep方法的,它只不过是帮我们进行了一下单位的换算,如下图:
最后,注意:sleep方法写在哪个线程中,它就会让那个线程休眠
yield方法
- 调用 yield 会让当前线程从 Running 进入 Runnable 就绪状态,然后调度执行其它线程
- 具体的实现依赖于操作系统的任务调度器
下面举个例子来说明一下
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class Test1 {
public static void main(String[] args) {
Thread thread1 = new Thread(()->{
int count = 0;
for (;;){
System.out.println("--->thread1 "+count++);
}
});
Thread thread2 = new Thread(()->{
int count = 0;
for (;;){
//这里使用yield来限制thread的时间片,
//使得thread1分到的时间片更多,我们可以通过打印的数字来看出
Thread.yield();
System.out.println(" ---->thread2 "+count++);
}
});
thread1.setName("thread1");
thread2.setName("thread2");
thread1.start();
thread2.start();
}
}
运行结果:
运行时间越长,即CPU越繁忙,yield方法的效果越明显。
线程优先级
- 线程优先级会提示(hint)调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略它(所以它不太靠谱);
- 总的来说就是数字越大优先级越高。
需要注意的一点
如果 CPU 比较忙,那么优先级高的线程会获得更多的时间片,但 CPU 闲时,优先级几乎没作用。
我们点进Thread类的源码里面可以找到关于线程优先级的三个变量,如下图:
MIN_PRIORITY = 1 是最低的优先级
NORM_PRIORITY = 5 是默认的优先级
MAX_PRIORITY = 10 是最高的优先级
我们举个例子来说明一下线程的优先级
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class Test1 {
public static void main(String[] args) {
Thread thread1 = new Thread(()->{
int count = 0;
for (;;){
System.out.println("--->thread1 "+count++);
}
});
Thread thread2 = new Thread(()->{
int count = 0;
for (;;){
System.out.println(" ---->thread2 "+count++);
}
});
thread1.setName("thread1");
thread2.setName("thread2");
//通过观察这里的运行结果,我们可以看到,如果运行的越久,
//即CPU越繁忙,则thread1被调用的几率大大减少,而thread2线程被调用的几率大大增加;
//反之,如果一运行就停止下来的话,他们两个被调用的几率其实没有太大的差别
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
}
运行结果:
可以看到,在CPU较繁忙的时候,线程优先级的作用就体现出来了。