首先我们要知道,在传统(操作系统)的线程模型中线程被分为五种状态
在java线程中,线程被分为六种状态
传统线程模型(操作系统)中线程状态
1.新建(new)
创建了一个新的线程对象
2.就绪(runnable)
调用线程的start()方法,处于就绪状态
3.运行(running)
获得了CPU时间片,执行程序代码
就绪状态是进入到运行状态的唯一入口
4.阻塞(block)
因为某种原因,线程放弃对CPU的使用权,停止执行,直到进入就绪状态在有可能再次被CPU调度
阻塞又分为三种:
1)等待阻塞:运行状态的线程执行wait方法,JVM会把线程放在等待队列中,使本线程进入阻塞状态。
2)同步阻塞:线程在获得synchronized同步锁失败,JVM会把线程放入锁池中,线程进入同步阻塞。对于锁池和等待池,可以看这篇文章
3)其他阻塞:调用线程的sleep()或者join()后,线程会进入道阻塞状态,当sleep超时或者join终止或超时,线程重新转入就绪状态
5.死亡(dead)
线程run()、main()方法执行结束,或者因为异常退出了run()方法,则该线程结束生命周期
死亡的线程不可再次复生
Java线程中的状态
通过查看Thread类的State方法,我们可以看到Java线程其实是六种状态
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
*
* - {@link Object#wait() Object.wait} with no timeout
* - {@link #join() Thread.join} with no timeout
* - {@link LockSupport#park() LockSupport.park}
*
*
* A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called Object.wait()
* on an object is waiting for another thread to call
* Object.notify() or Object.notifyAll() on
* that object. A thread that has called Thread.join()
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
*
* - {@link #sleep Thread.sleep}
* - {@link Object#wait(long) Object.wait} with timeout
* - {@link #join(long) Thread.join} with timeout
* - {@link LockSupport#parkNanos LockSupport.parkNanos}
* - {@link LockSupport#parkUntil LockSupport.parkUntil}
*
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
我们可以看到线程实际上是分为六种状态的,既
线程被构建,但是还没有调用start方法
Java线程把操作系统中就绪和运行两种状态统一称为“运行中”
表示线程进入等待状态,也就是线程因为某种原因放弃了CPU的使用权,阻塞也分为几种情况(当一个线程试图获取一个内部的对象锁(非java.util.concurrent库中的锁),而该锁被其他线程持有,则该线程进入阻塞状态。)
等待阻塞:运行的线程执行了Thread.sleep、wait、join等方法,JVM会把当前线程设置为等待状态,当sleep结束,join线程终止或者线程被唤醒后,该线程从等待状态进入阻塞状态,重新占用锁后进行线程恢复
同步阻塞:运行的线程在获取对象的同步锁时,若该同步锁被其他线程锁占用了,那么JVM会把当前项城放入到锁池中
其他阻塞:发出I/O请求,JVM会把当前线程设置为阻塞状态,当I/O处理完毕则线程恢复
等待状态,没有超时时间(无限等待),要被其他线程或者有其他的中断操作
执行wait、join、LockSupport.park()
与等待不同的是,不是无限等待,超时后自动返回
执行sleep,带参数的wait等可以实现
代表线程执行完毕
线程的运行流程
线程首先被new创建,进入初始状态
然后线程调用start方法,进入就绪状态
这里要注意,线程只要抢占了cpu时间片,可以不用获取全部的锁就可以运行,但是当运行到需要的锁没有获得时,会进入阻塞状态
当一个线程被sleep后,线程会先进入超时等待状态,当时间结束后,会先进入等待阻塞状态,当有锁以后再进入就绪状态
参考文章
Java线程基础 - (二)线程的状态详解(对比三种、五种和六种状态)_reading-conference-writing-CSDN博客一文搞懂线程世界级难题——线程状态到底是6种还是5种!!!_henry_2016的博客-CSDN博客_线程状态是5种还是6种