Java线程状态
2021/11/17 20:40:44
本文主要是介绍Java线程状态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- 1、Java线程6中状态
- 2、Tread类中State枚举定义的6种状态
- 3、各种状态的图解
- 4、各种状态的解释
1、Java线程6中状态
Java线程一共分为6种状态,如下所示
- NEW:新建状态
- RUNNABLE:就绪状态
- BLOCKED:阻塞状态
- WAITING:等待状态
- TIMED_WAITING:限时等待状态
- TERMINATED:结束状态
2、Tread类中State枚举定义的6种状态
通过Tread中的State枚举查看这六种状态的定义,如下所示:
public enum State { /** * Thread state for a thread which has not yet started. */ NEW, /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE, /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }
3、各种状态的图解
各种状态图解,如下图所示:
4、各种状态的解释
-
NEW状态
表示刚刚创建的线程,这种线程还没开始执行。等到线程的start()方法调用时,才表示线程开始执行。 -
RUNNABLE状态
表示线程所需的一切资源都已经准备好了。处于就绪状态的线程,只是说明此线程已经做好了被运行的准备,随时等待CPU调度执行,而并不是说调用了start()方法,这个线程就会被立即运行。 -
BLOCKED状态
如果线程在执行过程中遇到了synchronized或者Lock,那么就会进入到阻塞状态。处于阻塞状态的线程会放弃对CPU的使用权,停止执行,直到次线程进入到了就绪的状态,才会有机会再次被CPU调用运行。 -
WAITING状态
如果线程被调用了没有超时时间的wait方法,那么会进入无限等待状态。直到被notify通知才可以继续执行。 -
TIMED_WAITING状态
与WAITING状态相似,区别是,在有限的时间内进行线程等待。
这篇关于Java线程状态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14后台交互资料入门指南
- 2024-11-14如何轻松创建项目环境:新手入门教程
- 2024-11-14如何抽离公共代码:初级开发者指南
- 2024-11-14Python编程入门指南
- 2024-11-14Python编程入门:如何获取参数
- 2024-11-14JWT 用户校验:简单教程与实践
- 2024-11-14Pre-commit 自动化测试入门指南
- 2024-11-14Python编程基础
- 2024-11-14Server Action入门教程:轻松掌握服务器操作
- 2024-11-14Server Component入门教程:轻松搭建服务器组件