1. 操作系统进程的五种状态
2. java线程的六种状态
Thread类中getState()方法可以获取线程的状态,返回值是Thread类中的enum类型,取值有NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED六种状态。
java的线程状态将阻塞状态细分为BLOCKED,WAITING,TIMED_WAITING,将就绪和运行统称为RUNNABLE。
- NEW:线程刚创建,还未启动时的状态
- RUNNABLE:java中将就绪和运行两种状态中统称为RUNNABLE状态
- BLOCKED:线程阻塞,等待获取锁
- WAITING:线程无限制等待
- TIMED_WAITING:线程有时限的等待
- TERMINATED:线程执行完毕,终止状态
网上找了一张状态图:
例子:
public class Test1 {
private static final Logger log = LoggerFactory.getLogger(Test1.class);
public static void main(String[] args) throws InterruptedException {
// 线程1 测试NEW, 未启动线程
Thread thread1 = new Thread("thread1") {
@Override
public void run() {}
};
// 线程2 测试RUNNABLE, 启动线程且一直在运行
Thread thread2 = new Thread("thread2") {
@Override
public void run() {
while(true){
}
}
};
thread2.start();
// 线程3 测试TIMED_WAITING, 休眠10秒,加锁
Thread thread3 = new Thread("thread3") {
@Override
public void run() {
synchronized (Test1.class){
try {
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread3.start();
// 线程4 测试WAITING,同步等待thread3
Thread thread4 = new Thread("thread4") {
@Override
public void run() {
标签:状态,java,Thread,六种,WAITING,线程,public
From: https://www.cnblogs.com/Aeons/p/18375500