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
- 阻塞状态(Blocked) 阻塞状态是线程因为某种原因放弃CPU使用权,进行上下文切换, 暂时停止运行。直到线程进入就绪状态,才有机会转到运行状态。阻塞的情况分三种:
- 等待阻塞:运行的线程执行wait()方法,JVM会把该线程放入等待池中。
- 同步阻塞:运行的线程在获取对象的同步锁时,若该同步锁被别的线程占用,则JVM会把该线程放入Entry Set中.
- 其他阻塞:运行的线程执行sleep()或join()方法,或者发出了I/O请求时,JVM会把该线程置为阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时,线程重新转入就绪状态。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)