【问题标题】:Thread state conflict inside run method; Why thread state is not "RUNNING"run方法内部的线程状态冲突;为什么线程状态不是“正在运行”
【发布时间】:2020-12-27 16:15:09
【问题描述】:

我正在做一些测试以了解 java 中的不同线程状态,但遇到了一些查询。

通常,当一个线程被实例化时,它被称为处于"NEW" 状态,然后当对其调用start() 方法时,操作系统调度程序获得控制并处于"RUNNABLE" 状态并且此外,当run()start() 在内部调用时,它被称为处于运行状态。

Thread.currentThread().getState() // Returns the state of the thread

但在执行以下代码时观察到,即使在 run 方法内进行测试,线程状态也不会显示为 "RUNNING"

谁能帮我理解为什么会这样?

public static void main(String[] args) {
        System.out.println("Hello World");
        Thread t=new Thread(()->{
            System.out.println("Hi");
            System.out.println(Thread.currentThread().getState());  //// STATE DISPLAYED AS "RUNNABLE" AGAIN
        });
        System.out.println(t.getState());  // STATE DISPLAYED AS "NEW"
        t.start();
        System.out.println(t.getState());  // STATE DISPLAYED AS "RUNNABLE"
}

lambda 表达式用于实现 run() 方法,我们正在测试线程的状态,该状态再次显示为“RUNNABLE”而不是“RUNNING”

【问题讨论】:

    标签: java multithreading concurrency parallel-processing


    【解决方案1】:

    因为RUNNING 状态不存在。如果您查看getState 方法的定义,您会看到以下内容:

    public State getState() {
        // get current thread state
        return jdk.internal.misc.VM.toThreadState(threadStatus);
    }
    

    如果你分析State是什么,你可以看到它是以下ENUM:

       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 {@code Object.wait()}
             * on an object is waiting for another thread to call
             * {@code Object.notify()} or {@code Object.notifyAll()} on
             * that object. A thread that has called {@code Thread.join()}
             * 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;
        }
    

    或来自 API Thread.State:

    public static enum Thread.State extends Enum 一个线程 状态。线程可以处于以下状态之一:

    尚未启动的线程处于此状态。

    RUNNABLE在 Java 虚拟机中执行的线程处于此状态。

    BLOCKED阻塞等待监视器锁的线程处于此状态。

    WAITING 无限期等待另一个线程执行特定操作的线程处于此状态。

    TIMED_WAITING 等待另一个线程执行操作达指定等待时间的线程处于此状态。

    TERMINATED 已退出的线程处于此状态。线程在给定点只能处于一种状态 时间。这些状态是虚拟机状态,不反映任何 操作系统线程状态。

    【讨论】:

    • 谢谢,我已经浏览了一些在线教程,例如javatpoint.com/life-cycle-of-a-thread,其中状态为“运行”状态。根据我的理解“RUNNABLE”是操作系统调度器的控制,如果这个理解是错误的,我们可以说“RUNNABLE”是操作系统的调度器获得控制然后开始执行它的状态吗?因为没有单独的“RUNNING”状态。
    • @Rakesh 从 Java 文档中,您实际上得到了一个很好的答案,“可运行线程的线程状态。处于可运行状态的线程正在 Java 虚拟机中执行,但它可能正在等待其他资源来自处理器等操作系统。”
    • @Rakesh,这样想,“RUNNABLE”意味着没有理由Java 运行时环境知道线程无法运行的原因。但是操作系统可能知道 JRE 不知道的原因(例如,当前所有 CPU 都分配给其他线程。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2016-04-25
    • 2011-12-12
    相关资源
    最近更新 更多