Java基础 --- 线程状态 Thread States

New Threads

  • 当用new符号创建线程时, 比如 new Thread(r). 这个线程还不在运行状态, 而属于new threads.
  • 在这个阶段, 会进行线程的资源分配

Runnable Threads

  • 当一个thread调用statr方法时, 这个线程处于 runnable 状态
  • 一个runnable状态的线程可能在也可能不在运行, 取决于操作系统有没有让这个线程运行
  • Java specification 没有区分 running和runnable, 一个running thread依然是runnable state
  • Always keep in mind that a runnable thread may or may not be running at any given time

Blocked and Waiting Threads

  • 当一个thread等待拿取锁时, 此进程进入blocked状态. 只有其他线程release掉锁, 并且scheduler将锁分配给此线程时. 此线程会被unblock
  • 当一个线程等待另一个线程通知将它唤醒时, 这个线程进入waiting状态
  • 有些方法有timeout参数, 使用这些方法将让线程进入timed waiting状态. 当计时结束时恢复. 这些方法包括: Thread.sleep and the timed versions of Object.wait, Thread.join, Lock.tryLock, and Condition.await

Terminated Threads

  • It dies a natural death because the run method exits normally. (包括使用interrupt)
  • It dies abruptly because an uncaught exception terminates the run method

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38803409/article/details/125480728
今日推荐