【3】线程的状态

Java中线程的状态分为6种。

  1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法。
  2. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running)两种状态笼统的称为"运行"。 线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取CPU的使用权,此时处于就绪状态(ready)。就绪状态的线程在获得CPU时间片后变为运行中状态(running)。
  3. 阻塞(BLOCKED):表示线程阻塞于锁。
  4. 等待(WAITING):进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断)。
  5. 超时等待(TIMED_WAITING):该状态不同于WAITING,它可以在指定的时间后自行返回。
  6. *终止(TERMINATED):表示该线程已经执行完毕。

这6种状态定义在Thread类的State枚举中,可查看源码进行一一对应。

php 复制代码
/**  
* A thread state. A thread can be in one of the following states:  
* <ul>  
* <li>{@link #NEW}<br>  
* A thread that has not yet started is in this state.  
* </li>  
* <li>{@link #RUNNABLE}<br>  
* A thread executing in the Java virtual machine is in this state.  
* </li>  
* <li>{@link #BLOCKED}<br>  
* A thread that is blocked waiting for a monitor lock  
* is in this state.  
* </li>  
* <li>{@link #WAITING}<br>  
* A thread that is waiting indefinitely for another thread to  
* perform a particular action is in this state.  
* </li>  
* <li>{@link #TIMED_WAITING}<br>  
* A thread that is waiting for another thread to perform an action  
* for up to a specified waiting time is in this state.  
* </li>  
* <li>{@link #TERMINATED}<br>  
* A thread that has exited is in this state.  
* </li>  
* </ul>  
*  
* <p>  
* A thread can be in only one state at a given point in time.  
* These states are virtual machine states which do not reflect  
* any operating system thread states.  
*  
* @since 1.5  
* @see #getState  
*/  
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:  
* <ul>  
* <li>{@link Object#wait() Object.wait} with no timeout</li>  
* <li>{@link #join() Thread.join} with no timeout</li>  
* <li>{@link LockSupport#park() LockSupport.park}</li>  
* </ul>  
*  
* <p>A thread in the waiting state is waiting for another thread to  
* perform a particular action.  
*  
* For example, a thread that has called <tt>Object.wait()</tt>  
* on an object is waiting for another thread to call  
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on  
* that object. A thread that has called <tt>Thread.join()</tt>  
* 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:  
* <ul>  
* <li>{@link #sleep Thread.sleep}</li>  
* <li>{@link Object#wait(long) Object.wait} with timeout</li>  
* <li>{@link #join(long) Thread.join} with timeout</li>  
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>  
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>  
* </ul>  
*/  
TIMED_WAITING,  
  
/**  
* Thread state for a terminated thread.  
* The thread has completed execution.  
*/  
TERMINATED;  
}

一、线程的状态图

二、状态详细说明

1. 初始状态(NEW)

实现Runnable接口和继承Thread可以得到一个线程类,new一个实例出来,线程就进入了初始状态。

java 复制代码
public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            System.out.println("我是一个多线程,我得名字是:"+Thread.currentThread().getName());
        },"秀儿");
        System.out.println(thread.getState().name());
}

2.1. 就绪状态(RUNNABLE之READY)

就绪状态只是说你资格运行,调度程序没有挑选到你,你就永远是就绪状态。 调用线程的start()方法,此线程进入就绪状态。 当前线程sleep()方法结束,其他线程join()结束,等待用户输入完毕,某个线程拿到对象锁,这些线程也将进入就绪状态。 当前线程时间片用完了,调用当前线程的yield()方法,当前线程进入就绪状态。 锁池里的线程拿到对象锁后,进入就绪状态。

2.2. 运行中状态(RUNNABLE之RUNNING)

线程调度程序从可运行池中选择一个线程作为当前线程时线程所处的状态。这也是线程进入运行状态的唯一的一种方式。

java 复制代码
public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            StringBuilder name = new StringBuilder();
            for (int i = 0; i < 1000000; i++) {
                name.append(i);
            }
        },"秀儿");
        thread.start();
        System.out.println(thread.getState().name());
}

3. 阻塞状态(BLOCKED)

阻塞状态是线程阻塞在进入synchronized关键字修饰的方法或代码块(获取锁)时的状态。

4. 等待(WAITING)

处于这种状态的线程不会被分配CPU执行时间,它们要等待被显式地唤醒,否则会处于无限期等待的状态。

5. 超时等待(TIMED_WAITING)

处于这种状态的线程不会被分配CPU执行时间,不过无须无限期等待被其他线程显示地唤醒,在达到一定时间后它们会自动唤醒。

6. 终止状态(TERMINATED)

当线程的run()方法完成时,或者主线程的main()方法完成时,我们就认为它终止了。这个线程对象也许是活的,但是它已经不是一个单独执行的线程。线程一旦终止了,就不能复生。 在一个终止的线程上调用start()方法,会抛出java.lang.IllegalThreadStateException异常。

三、等待队列

  • 调用obj的wait(), notify()方法前,必须获得obj锁,也就是必须写在synchronized(obj) 代码段内。
  • 与等待队列相关的步骤和图
  • 线程1获取对象A的锁,正在使用对象A。
  • 线程1调用对象A的wait()方法。
  • 线程1释放对象A的锁,并马上进入等待队列。
  • 锁池里面的对象争抢对象A的锁。
  • 线程5获得对象A的锁,进入synchronized块,使用对象A。
  • 线程5调用对象A的notifyAll()方法,唤醒所有线程,所有线程进入同步队列。若线程5调用对象A的notify()方法,则唤醒一个线程,不知道会唤醒谁,被唤醒的那个线程进入同步队列。
  • notifyAll()方法所在synchronized结束,线程5释放对象A的锁。
  • 同步队列的线程争抢对象锁,但线程1什么时候能抢到就不知道了。

四、同步队列状态

  • 当前线程想调用对象A的同步方法时,发现对象A的锁被别的线程占有,此时当前线程进入同步队列。简言之,同步队列里面放的都是想争夺对象锁的线程。
  • 当一个线程1被另外一个线程2唤醒时,1线程进入同步队列,去争夺对象锁。
  • 同步队列是在同步的环境下才有的概念,一个对象对应一个同步队列。
  • 线程等待时间到了或被notify/notifyAll唤醒后,会进入同步队列竞争锁,如果获得锁,进入RUNNABLE状态,否则进入BLOCKED状态等待获取锁。

几个方法的比较

  • Thread.sleep(long millis),一定是当前线程调用此方法,当前线程进入TIMED_WAITING状态,但不释放对象锁,millis后线程自动苏醒进入就绪状态。作用:给其它线程执行机会的最佳方式。
  • Thread.yield(),一定是当前线程调用此方法,当前线程放弃获取的CPU时间片,但不释放锁资源,由运行状态变为就绪状态,让OS再次选择线程。作用:让相同优先级的线程轮流执行,但并不保证一定会轮流执行。实际中无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度程序再次选中。
  • Thread.yield()不会导致阻塞。该方法与sleep()类似,只是不能由用户指定暂停多长时间。
  • thread.join()/thread.join(long millis),当前线程里调用其它线程t的join方法,当前线程进入WAITING/TIMED_WAITING状态,当前线程不会释放已经持有的对象锁。线程t执行完毕或者millis时间到,当前线程一般情况下进入RUNNABLE状态,也有可能进入BLOCKED状态(因为join是基于wait实现的)。
  • obj.wait(),当前线程调用对象的wait()方法,当前线程释放对象锁,进入等待队列。依靠
  • notify()/notifyAll()唤醒或者wait(long timeout) timeout时间到自动唤醒。
  • obj.notify()唤醒在此对象监视器上等待的单个线程,选择是任意性的。notifyAll()唤醒在此对象监视器上等待的所有线程。
  • LockSupport.park()/LockSupport.parkNanos(long nanos),LockSupport.parkUntil(long deadlines), 当前线程进入WAITING/TIMED_WAITING状态。对比wait方法,不需要获得锁就可以让线程进入WAITING/TIMED_WAITING状态,需要通过LockSupport.unpark(Thread thread)唤醒。
相关推荐
ok!ko3 小时前
设计模式之原型模式(通俗易懂--代码辅助理解【Java版】)
java·设计模式·原型模式
2401_857622663 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
2402_857589363 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰4 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
哎呦没4 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
_.Switch5 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
编程、小哥哥5 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程6 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
莹雨潇潇6 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
杨哥带你写代码6 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端