1.线程状态的分类
2.每种线程状态表示的含义
3.线程之间的切换条件
线程的状态有:
1.NEW :表示Thread对象已经创建,但内核pcb还没有创建
2.TERMINATED:内核pcb已经销毁,但Thread对象还存在
//就绪
3.RUNNABLE:就绪状态,代表正在pcb上运行+在就绪队列中排队
//阻塞
4.TIME_WAITING:按照一定的时间进行阻塞(sleep)
5.WAITING:特殊阻塞(wait)
6.BLOCKED:等待锁的时候进入阻塞状态(synchronized)

上图为各种状态的基本关系
- //用getState获取线程的各种状态
- public class Demo8 {
- public static void main(String[] args) throws InterruptedException {
- Thread thread = new Thread(() -> {
- for(int i = 0; i < 10_0000_0000; i++){
- //啥也不干
- }
-
- });
- //在start之前是new,对象创建,但内核pcb还未创建
- System.out.println(thread.getState());
-
- //开始
- thread.start();
-
- //线程正在运行
- System.out.println(thread.getState());
-
- //结束
- thread.join();
- //join之后该线程已经结束,状态为Terminated
- System.out.println(thread.getState());
-
- }
- }
该代码为new,terminated,和runnable三种线程从开始到结束的过程

time_waiting
