Java 实例 - 获取线程状态

Java 线程的生命周期中,在 Thread 类里有一个枚举类型 State,定义了线程的几种状态,分别有:

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated

Java 实例 - 获取线程状态 - 图1

各个状态说明:

1. 初始状态 - NEW

声明:

  1. public static final Thread.State NEW

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

2. RUNNABLE

声明:

  1. public static final Thread.State RUNNABLE

2.1. 就绪状态

就绪状态只是说你资格运行,调度程序没有挑选到你,你就永远是就绪状态。

调用线程的 start() 方法,此线程进入就绪状态。

当前线程 sleep() 方法结束,其他线程 join() 结束,等待用户输入完毕,某个线程拿到对象锁,这些线程也将进入就绪状态。

当前线程时间片用完了,调用当前线程的 yield() 方法,当前线程进入就绪状态。

锁池里的线程拿到对象锁后,进入就绪状态。

2.2. 运行中状态

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

3. 阻塞状态 - BLOCKED

声明:

  1. public static final Thread.State BLOCKED

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

4. 等待 - WAITING

声明:

  1. public static final Thread.State WAITING

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

5. 超时等待 - TIMED_WAITING

声明:

  1. public static final Thread.State TIMED_WAITING

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

6. 终止状态 - TERMINATED

声明:

  1. public static final Thread.State TERMINATED

当线程的 run() 方法完成时,或者主线程的 main() 方法完成时,我们就认为它终止了。这个线程对象也许是活的,但是,它已经不是一个单独执行的线程。线程一旦终止了,就不能复生。

在一个终止的线程上调用 start() 方法,会抛出 java.lang.IllegalThreadStateException 异常。

以下实例演示了如何获取线程的状态:

Main.java 文件

  1. // Java 程序 - 演示线程状态
  2. class thread implements Runnable
  3. {
  4. public void run()
  5. {
  6. // thread2 - 超时等待
  7. try
  8. {
  9. Thread.sleep(1500);
  10. }
  11. catch (InterruptedException e)
  12. {
  13. e.printStackTrace();
  14. }
  15. System.out.println("State of thread1 while it called join() method on thread2 -"+
  16. Test.thread1.getState());
  17. try
  18. {
  19. Thread.sleep(200);
  20. }
  21. catch (InterruptedException e)
  22. {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. public class Test implements Runnable
  28. {
  29. public static Thread thread1;
  30. public static Test obj;
  31. public static void main(String[] args)
  32. {
  33. obj = new Test();
  34. thread1 = new Thread(obj);
  35. // 创建 thread1,现在是初始状态
  36. System.out.println("State of thread1 after creating it - " + thread1.getState());
  37. thread1.start();
  38. // thread1 - 就绪状态
  39. System.out.println("State of thread1 after calling .start() method on it - " +
  40. thread1.getState());
  41. }
  42. public void run()
  43. {
  44. thread myThread = new thread();
  45. Thread thread2 = new Thread(myThread);
  46. // 创建 thread1,现在是初始状态
  47. System.out.println("State of thread2 after creating it - "+ thread2.getState());
  48. thread2.start();
  49. // thread2 - 就绪状态
  50. System.out.println("State of thread2 after calling .start() method on it - " +
  51. thread2.getState());
  52. // moving thread1 to timed waiting state
  53. try
  54. {
  55. //moving - 超时等待
  56. Thread.sleep(200);
  57. }
  58. catch (InterruptedException e)
  59. {
  60. e.printStackTrace();
  61. }
  62. System.out.println("State of thread2 after calling .sleep() method on it - "+
  63. thread2.getState() );
  64. try
  65. {
  66. // 等待 thread2 终止
  67. thread2.join();
  68. }
  69. catch (InterruptedException e)
  70. {
  71. e.printStackTrace();
  72. }
  73. System.out.println("State of thread2 when it has finished it's execution - " +
  74. thread2.getState());
  75. }
  76. }

以上代码运行输出结果为:

  1. State of thread1 after creating it - NEW
  2. State of thread1 after calling .start() method on it - RUNNABLE
  3. State of thread2 after creating it - NEW
  4. State of thread2 after calling .start() method on it - RUNNABLE
  5. State of thread2 after calling .sleep() method on it - TIMED_WAITING
  6. State of thread1 while it called join() method on thread2 -WAITING
  7. State of thread2 when it has finished it's execution - TERMINATED