6

When a thread is RUNNABLE, it can be running, or be not. Is there a way to distinguish it from java.lang.Thread.State#RUNNABLE?

All states of threads in 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());
    }
}

The output:

RUNNABLE
BaiJiFeiLong
  • 3,716
  • 1
  • 30
  • 28
  • `isAlive()` to be used here. – Doc Apr 07 '19 at 12:15
  • you may want to read https://stackoverflow.com/questions/17293304/when-is-a-java-thread-alive – Doc Apr 07 '19 at 12:18
  • how are you going to use this information? Suppose you learned that your thread is really running and decide to take some action. But O/S may in next microsecond move the thread from processor, and your action would be wrong. – Alexei Kaigorodov Apr 07 '19 at 15:23

3 Answers3

3

As you sayed, a runnable thread may or may not actually be running. It is up to the operating system to give the thread time to run. The Java specification does not call this as a separate state, though. A running thread is still in the runnable state and there is not possible to distinguish these two states via java in a simple way.

Abolfazl Hashemi
  • 684
  • 7
  • 21
1

Suppose that the Thread class defined a function, boolean isRunning(). What could you do with it? This, for example, would be meaningless:

if (t.isRunning()) {
    doXYandZ();
}

The problem with it is, even if thread t actually was RUNNING at some point during the isRunning() call, there's no guarantee that it will still be running by the time t.isRunning() returns true. The is no guarantee that it won't stop running sometime during the doXYandZ() call.

The operating system is able to distinguish between threads that are RUNNING and threads that are not because that state is controlled by the operating system, and the operating system needs to remember which threads are RUNNING and which are not because it's the operating system's job to schedule them.

The Java Runtime Environment doesn't distinguish between RUNNING and RUNNABLE because it doesn't control which threads are are actually RUNNING at any given time, and it would have no way to meaningfully react to changes in that state.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
0

Because java thread depends on os thread.OS example,linux process status R(task_running)is contain running and scheduled to run in the run queue(ready status)

chen kqing
  • 29
  • 5