多线程
5 线程状态
5.5 线程状态观测
- Thread.State
- 线程可以处于以下状态之一:
- NEW:尚未启动的线程处于此状态
- RUNNABLE:在Java虚拟机中执行的线程处于此状态
- BLOCKED:被阻塞等待监视器锁定的线程处于此状态
- WAITING:正在等待另一个线程执行特定动作的线程处于此状态
- TIMED_WAITING:正在等待另一个线程执行动作达到指定等待时间的线程处于此状态
- TERMINATED:已退出的线程处于此状态
- 一个线程可以在给定时间点处于一个状态。这些状态是不反映任何操作系统线程状态的虚拟机状态。
package com.duo.state;
//观察测试线程的状态
public class StateTest {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000); //-->TIMED_WAITING
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("----------");
});
Thread.State state = thread.getState();
System.out.println(state); //-->NEW
thread.start();
state = thread.getState();
System.out.println(state); //-->RUNNABLE
while (state != Thread.State.TERMINATED) {
Thread.sleep(500);
state = thread.getState();
System.out.println(state);
}
}
}
运行结果:
可以看到,观测到的线程状态如期输出。
5.6 线程优先级
- Java提供了一个线程调度器,监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行
- 线程的优先级用数字表示,范围由1~10.
- Thread.MIN_PRIORITY = 1
- Thread.MAX_PRIORITY = 10
- Thread.NORM_PRIORITY = 5
- 使用以下方式改变或获取优先级:
- getPriority() setPriority(int xx)
- 优先级的设定建议在start()调度之前
- 优先级低只是意味着获得调度的概率低,并非优先级低就不会被调用,这一切都取决于CPU的调度!
package com.duo.state;
//测试线程的优先级
public class PriorityTest {
public static void main(String[] args) {
//先打印输出主线程默认优先级
System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread thread1 = new Thread(myPriority);
Thread thread2 = new Thread(myPriority);
Thread thread3 = new Thread(myPriority);
Thread thread4 = new Thread(myPriority);
Thread thread5 = new Thread(myPriority);
thread1.start();
thread2.setPriority(2);
thread2.start();
thread3.setPriority(Thread.MAX_PRIORITY);
thread3.start();
thread4.setPriority(Thread.MIN_PRIORITY);
thread4.start();
thread5.setPriority(8);
thread5.start();
}
}
class MyPriority implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getPriority());
}
}
运行结果:
如上所示,线程并非严格按照设定的优先级执行,这也证明上述第五点,即优先级低只是意味着获得调度的概率低,并非优先级低就不会被调用,这一切都取决于CPU的调度!
5.7 守护(daemon)线程
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 例如,后台记录操作日志、监控内存、垃圾回收等等
package com.duo.state;
//测试守护线程
public class DaemonTest {
public static void main(String[] args) {
Family family = new Family();
Role role = new Role();
Thread thread = new Thread(family);
thread.setDaemon(true); //默认是false的,设置为true表示用户线程;正常的线程都是用户线程
thread.start(); //守护线程启动
new Thread(role).start(); //用户线程启动
}
}
class Family implements Runnable {
@Override
public void run() {
while (true) System.out.println("家人守护着你");
}
}
class Role implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 3000; i++) {
System.out.println("开心度过了第" + i + "天");
}
System.out.println("=====Goodbye, world!=====");
}
}
运行结果:
可以看到,虚拟机在确保用户线程执行完毕后,并没有等待守护线程执行完毕(此例中守护线程代码体设置为了死循环),但在守护线程执行完后,守护线程并没有立即停止执行,而是继续执行了一段时间,这也表明关闭虚拟机需要一定时间。