多线程--线程状态

待我称王封你为后i 2024-02-19 14:41 162阅读 0赞

线程对象在不同的运行期间有不同的状态,准确的把握线程状态,能更好的去控制线程的运行,其状态信息就保存在Thread类中的枚举State中。

  1. /**
  2. * A thread state. A thread can be in one of the following states:
  3. * <ul>
  4. * <li>{@link #NEW}<br>
  5. * A thread that has not yet started is in this state.
  6. * </li>
  7. * <li>{@link #RUNNABLE}<br>
  8. * A thread executing in the Java virtual machine is in this state.
  9. * </li>
  10. * <li>{@link #BLOCKED}<br>
  11. * A thread that is blocked waiting for a monitor lock
  12. * is in this state.
  13. * </li>
  14. * <li>{@link #WAITING}<br>
  15. * A thread that is waiting indefinitely for another thread to
  16. * perform a particular action is in this state.
  17. * </li>
  18. * <li>{@link #TIMED_WAITING}<br>
  19. * A thread that is waiting for another thread to perform an action
  20. * for up to a specified waiting time is in this state.
  21. * </li>
  22. * <li>{@link #TERMINATED}<br>
  23. * A thread that has exited is in this state.
  24. * </li>
  25. * </ul>
  26. *
  27. * <p>
  28. * A thread can be in only one state at a given point in time.
  29. * These states are virtual machine states which do not reflect
  30. * any operating system thread states.
  31. *
  32. * @since 1.5
  33. * @see #getState
  34. */
  35. public enum State {
  36. /**
  37. * Thread state for a thread which has not yet started.
  38. */
  39. NEW,
  40. /**
  41. * Thread state for a runnable thread. A thread in the runnable
  42. * state is executing in the Java virtual machine but it may
  43. * be waiting for other resources from the operating system
  44. * such as processor.
  45. */
  46. RUNNABLE,
  47. /**
  48. * Thread state for a thread blocked waiting for a monitor lock.
  49. * A thread in the blocked state is waiting for a monitor lock
  50. * to enter a synchronized block/method or
  51. * reenter a synchronized block/method after calling
  52. * {@link Object#wait() Object.wait}.
  53. */
  54. BLOCKED,
  55. /**
  56. * Thread state for a waiting thread.
  57. * A thread is in the waiting state due to calling one of the
  58. * following methods:
  59. * <ul>
  60. * <li>{@link Object#wait() Object.wait} with no timeout</li>
  61. * <li>{@link #join() Thread.join} with no timeout</li>
  62. * <li>{@link LockSupport#park() LockSupport.park}</li>
  63. * </ul>
  64. *
  65. * <p>A thread in the waiting state is waiting for another thread to
  66. * perform a particular action.
  67. *
  68. * For example, a thread that has called <tt>Object.wait()</tt>
  69. * on an object is waiting for another thread to call
  70. * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
  71. * that object. A thread that has called <tt>Thread.join()</tt>
  72. * is waiting for a specified thread to terminate.
  73. */
  74. WAITING,
  75. /**
  76. * Thread state for a waiting thread with a specified waiting time.
  77. * A thread is in the timed waiting state due to calling one of
  78. * the following methods with a specified positive waiting time:
  79. * <ul>
  80. * <li>{@link #sleep Thread.sleep}</li>
  81. * <li>{@link Object#wait(long) Object.wait} with timeout</li>
  82. * <li>{@link #join(long) Thread.join} with timeout</li>
  83. * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
  84. * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
  85. * </ul>
  86. */
  87. TIMED_WAITING,
  88. /**
  89. * Thread state for a terminated thread.
  90. * The thread has completed execution.
  91. */
  92. TERMINATED;
  93. }

线程的状态改变的主要原因是线程的调用与线程相关方法的运行。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU0OTU3OA_size_16_color_FFFFFF_t_70

NEW:线程实例化还没有执行start()方法时的状态。

RUNNABLE:线程进入运行的状态。

TERMINATED:线程被销毁时的状态。

TIMED_WAITING:线程执行了Thread.sleep()方法,呈等待状态,时间到达,继续向下运行。

BLOCKED:线程阻塞,某个线程在等待锁的时候。

WAITING:线程执行了Object.wait()方法后的状态。

发表评论

表情:
评论列表 (有 0 条评论,162人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java线 —— 线状态迁移

    引言 线程状态迁移,又常被称作线程的生命周期,指的是线程从创建到终结需要经历哪些状态,什么情况下会出现哪些状态。 线程的状态直接关系着并发编程的各种问题,本文就线程的状

    相关 线之——线状态

    线程状态 线程是相对独立的、可调度的执行单元,因为在线程的执行过程中,会分别处在不同的状态。通常而言,线程主要有下列几种运行状态: 1.就绪状态: 即线程已经具备了