如何在Java中区分RUNNING状态与java.lang.Thread.State.RUNNABLE
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java中区分RUNNING状态与java.lang.Thread.State.RUNNABLE,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1615字,纯文字阅读大概需要3分钟。
内容图文
![如何在Java中区分RUNNING状态与java.lang.Thread.State.RUNNABLE](/upload/InfoBanner/zyjiaocheng/650/342340b5d0d94870952ed1c915e0eecb.jpg)
当线程是RUNNABLE时,它可以正在运行,也可以不运行.有没有办法将其与java.lang.Thread.State#RUNNABLE区别开?
Java doc中线程的所有状态:
java.lang public class Thread.State
extends Enum
A thread state. A thread can be in one of the following states:
NEW A thread that has not yet started is in this state.
RUNNABLE A thread executing in the Java virtual machine is in this state.
BLOCKED A thread that is blocked waiting for a monitor lock is in this state.
WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED A thread that has exited is in this state.
A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.
package bj.thread;
public class ThreadApp2 {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getState());
}
}
输出:
RUNNABLE
解决方法:
假设Thread类定义了一个函数boolean isRunning().你能用它做什么?例如,这将毫无意义:
if (t.isRunning()) {
doXYandZ();
}
它的问题是,即使线程t实际上在isRunning()调用期间的某个时刻正在运行,也无法保证在t.isRunning()返回true时它仍将在运行.无法保证在doXYandZ()调用期间它不会停止运行.
操作系统能够区分正在运行的线程和不是由于该状态由操作系统控制的线程,并且操作系统需要记住哪些线程正在运行,哪些不是因为计划的是操作系统的工作他们.
Java运行时环境无法区分RUNNING和RUNNABLE,因为它无法控制在任何给定时间实际正在运行哪些线程,并且它无法对该状态的变化做出有意义的反应.
内容总结
以上是互联网集市为您收集整理的如何在Java中区分RUNNING状态与java.lang.Thread.State.RUNNABLE全部内容,希望文章能够帮你解决如何在Java中区分RUNNING状态与java.lang.Thread.State.RUNNABLE所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。