生命周期图

image.png

线程的 7 种状态

JDK 中用 Thread.State 枚举类定义了线程的几种状态,想要实现多线程,必须在主线程中创建新的线程对象。

Java 语言使用 Thread 及其子类的对象来表示线程,在它的一个完整的生命周期中通常要经历如下的 5 种状态。

NEW
1)新建
当一个Thread类或其子类的对象被声明并创建时,此时线程尚未启动,新生的线程对象处于新建状态。

RUNNABLE
2)就绪 ready
处于新建状态的线程被 start() 后,将进入线程队列等待 CPU 时间片,此时它已具备了运行的条件,只是没分配到 CPU 资源。

3)运行 running
当就绪的线程被调度并获得 CPU 资源时,便进入运行状态,run() 方法定义了线程的操作和功能。

BLOCKED
4)阻塞
进入一个同步块/方法前,处于阻塞状态的线程正在等待锁,或者在调用 Object.wait 后重新进入一个同步块/方法。阻塞时,线程让出 CPU 并临时终止自己的执行。

WAITING
5)等待中
一个等待中的线程的状态。一个线程由于调用了以下方法之一而处于等待状态。

  • 没有设置超时时间的 Object.wait;
  • 没有设置超时时间的 Thread.join;
  • LockSupport.park

一个处于等待状态的线程正在等待另一个线程执行一个特定的动作。例如,一个在对象上调用了 Object.wait() 的线程正在等待另一个线程对该对象调用 Object.notify() 或 Object.notifyAll() 。一个调用了 Thread.join() 的线程正在等待一个指定的线程终止。

TIMED_WAITING**
6)定时等待
一个具有指定等待时间的等待线程的线程状态。一个线程由于以指定的正数等待时间调用了以下方法之一而处于定时的等待状态。

  • Thread.sleep
  • Object.wait with timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil

TERMINATED
7)终止
线程完成了它的全部工作或线程被提前强制性地终止或出现异常导致结束

Thread.State 源码

  1. public enum State {
  2. /**
  3. * Thread state for a thread which has not yet started.
  4. */
  5. NEW,
  6. /**
  7. * Thread state for a runnable thread. A thread in the runnable
  8. * state is executing in the Java virtual machine but it may
  9. * be waiting for other resources from the operating system
  10. * such as processor.
  11. */
  12. RUNNABLE,
  13. /**
  14. * Thread state for a thread blocked waiting for a monitor lock.
  15. * A thread in the blocked state is waiting for a monitor lock
  16. * to enter a synchronized block/method or
  17. * reenter a synchronized block/method after calling
  18. * {@link Object#wait() Object.wait}.
  19. */
  20. BLOCKED,
  21. /**
  22. * Thread state for a waiting thread.
  23. * A thread is in the waiting state due to calling one of the
  24. * following methods:
  25. * <ul>
  26. * <li>{@link Object#wait() Object.wait} with no timeout</li>
  27. * <li>{@link #join() Thread.join} with no timeout</li>
  28. * <li>{@link LockSupport#park() LockSupport.park}</li>
  29. * </ul>
  30. *
  31. * <p>A thread in the waiting state is waiting for another thread to
  32. * perform a particular action.
  33. *
  34. * For example, a thread that has called {@code Object.wait()}
  35. * on an object is waiting for another thread to call
  36. * {@code Object.notify()} or {@code Object.notifyAll()} on
  37. * that object. A thread that has called {@code Thread.join()}
  38. * is waiting for a specified thread to terminate.
  39. */
  40. WAITING,
  41. /**
  42. * Thread state for a waiting thread with a specified waiting time.
  43. * A thread is in the timed waiting state due to calling one of
  44. * the following methods with a specified positive waiting time:
  45. * <ul>
  46. * <li>{@link #sleep Thread.sleep}</li>
  47. * <li>{@link Object#wait(long) Object.wait} with timeout</li>
  48. * <li>{@link #join(long) Thread.join} with timeout</li>
  49. * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
  50. * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
  51. * </ul>
  52. */
  53. TIMED_WAITING,
  54. /**
  55. * Thread state for a terminated thread.
  56. * The thread has completed execution.
  57. */
  58. TERMINATED;
  59. }

查看线程当前状态

  1. public class Main {
  2. public static void main(String[] args) {
  3. Task task = new Task();
  4. task.setName("铸剑计划");
  5. System.out.println(task.getState());
  6. task.start();
  7. System.out.println(task.getState());
  8. while (task.getState() != Thread.State.TERMINATED){
  9. System.out.println(task.getState());
  10. try {
  11. Thread.sleep(500);
  12. } catch (InterruptedException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. System.out.println(task.getState());
  17. }
  18. }
  19. class Task extends Thread {
  20. @Override
  21. public void run() {
  22. for (int i = 0; i < 3; i++) {
  23. try {
  24. sleep(1000);
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. System.out.println(getName() + ": " + i);
  29. }
  30. }
  31. }