java - Java中如何区分RUNNING状态和java.lang.Thread.State.RUNNABLE

标签 java multithreading

当线程RUNNABLE 时,它可以运行,也可以不运行。有没有办法将它与 java.lang.Thread.State#RUNNABLE 区分开来?

Java文档中线程的所有状态:

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

最佳答案

如您所说,可运行线程实际上可能正在运行,也可能未运行。由操作系统给线程运行时间。 不过,Java 规范并未将此称为单独的状态。一个正在运行的线程仍然处于可运行状态,无法通过 java 以简单的方式区分这两种状态

关于java - Java中如何区分RUNNING状态和java.lang.Thread.State.RUNNABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55558854/

相关文章:

c++ - 在 OpenMP C++ 中使用并行 block 时没有看到任何显着改进

android - IntentService 或 JobScheduler 在 onPause 期间执行 I/O 密集型数据保存操作

java - 将 JDK 放在 ramdisk 上如何提高 IDE 性能?

java - 如何通过类和方法传递数组和 int ?

方法参数列表的Java 8并行流

java - 为什么我的springMVC无法从前端页面获取模型?

java - Android TabLayout使用listfragment向上滚动动画

python - 带信号灯的异步/多线程

Java是通过传递循环并提前运行代码吗?

c++ - 结合使用 std::atomic<bool> 和 std::mutex 的正确性