线程状态对应汇总(BASH ps命令、Linux内核 、Java jdk线程状态)

BASH ps 命令

ps -l


UID/PID/PPID: 代表『此进程被该 UID 所拥有/进程的 PID 号/此进程的父进程 PID 』
F:代表这个进程旗标 (process flags),说明这个进程的权限,常见有:若4 表示此进程的权限 root ;若1 則表示此子进程仅能fork。
CPU:代表 CPU 使用率,单位为百分比;
PRI/NI:Priority/Nice 的缩写,代表此进程被 CPU 所执行的优先顺序,数值越小代表该进程越快被 CPU 执行。
RSS:实际内存占用大小 单位KB
ADDR/SZ/WCHAN:都与内存相关,ADDR 是 kernel function,指出该进程在内存的哪個部分,如果是个 running 进程,一般就会显示『 - 』 / SZ 代表此进程用掉多少内存/ WCHAN 表示目前进程是否工作,同样的, 若为 - 表示正在工作中。
S:代表这个进程的状态 (STAT),主要的状态有:R (Running):该进程正在运行;S (Sleep):该进程正在睡眠,可被唤醒。D :不可被唤醒T :停止状态(stop);Z (Zombie):僵尸进程。
TTY:登入者的终端机位置,若为远程登入则使用动态終端介面 (pts/n);
TIME:使用掉的 CPU 时间,注意,是实际花费掉的 CPU 运作的时间,而不是系統时间;
CMD:就是 command 的缩写,造成此进程的指令。

Linux操作系统bash命令列表 中的ps对应文档

ps命令 PROCESS STATE CODES 解释 内核状态 Java线程状态
D uninterruptible sleep (usually IO)不可中断睡眠 (通常是在IO操作) 收到信号不唤醒和不可运行, 进程必须等待直到有中断发生 不可中断TASK_UNINTERRUPTIBLE 阻塞态(?)BLOCKED
I Idle kernel thread 空闲线程,优先级最低,作用就是保证至少有一个活跃的线程在内核中
R running or runnable (on run queue)正在运行或可运行(在运行队列排队中) 可运行TASK_RUNNING 运行态RUNNABLE
S interruptible sleep (waiting for an event to complete)可中断睡眠 (休眠中, 受阻, 在等待某个条件的形成或接受到信号) 可中断TASK_INTERRUPTIBLE 阻塞态 BLOCKED 等待态WAITING
T stopped by job control signal已停止的 进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行 停止TASK_STOPPED 终止态TERMINATED
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)正在换页(2.6.内核之前有效)
X dead (should never be seen)死进程(未开启)
Z a defunct (“zombie”) process进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放 僵死TASK_ZOMBIE 终止态TERMINATED
For BSD formats and when the “stat” keyword is used, additional letters can be displayed:
+ s in the foreground process group在前台进程组
< 高优先级(not nice to other users)
N 低优先级(nice to other users)
L has pages locked into memory (for real-time and custom IO)页面锁定在内存(实时和定制的IO)
s is a session leader 一个信息头
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)多线程(使用 CLONE_THREAD,像NPTL的pthreads的那样)

Linux进程状态

TASK_RUNNING可运行状态。进程正在运行(已获得CPU)或准备运行(等待获得CPU),由run_list()把所有运行态进程链接起来。LinuxV2.6内核改为每个CPU单独维护一个进程运行队列。

TASK_INTERRUPTIBLE可中断等待状态。进程进入等待队列中,一旦有资源可用时进程就被唤醒,也可由其他进程通过信号或中断来唤醒。

TASK_UNINTERRUPTIBLE不可中断等待状态。进程排入等待队列中,一旦资源可用时进程就被唤醒,但是不可由信号或中断来唤醒,此状态通常意味着进程在等待某些硬件条件,如IO。这种状态通常在进程必须等待时不受干扰或等待事件很快就会发生时出现。由于此状态的任务对信号不做响应,就是在ps命令中那些D状态的进程不能被杀死的原因。

TASK_ZOMBIE僵死状态。进程运行完成,已释放所占大部分资源,尚未释放task_struct结构体。大部分是子进程结束,等待父进程wait()

TASK_STOPPED暂停状态。此状态用于调试,因收到SIGSTOP, SIGTSTP,SIGTTIN,SIGTTOU信号而暂停执行,也可能受到其他进程的跟踪和调用而暂时让出CPU。
在这里插入图片描述


JAVA jdk线程状态

Java线程状态在Thread中定义,源码中能看到有个枚举State,总共定义了六种状态:
源码如下:

/**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * 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.
     *
     * @since   1.5
     * @see #getState
     */
    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;
    }

NEW: 新建状态,线程对象已经创建,但尚未启动

RUNNABLE: 就绪状态,可运行状态,调用了线程的start方法,已经在java虚拟机中执行,等待获取操作系统资源如CPU,操作系统调度运行。ready+running的统称

BLOCKED: 堵塞状态。线程等待锁的状态,等待获取锁进入同步块/方法或调用wait后重新进入需要竞争锁

WAITING: 等待状态。等待另一个线程以执行特定的操作。调用以下方法进入等待状态。Object.wait(),Thread.join(),LockSupport.park

TIMED_WAITING: 超时等待。调用带参数的Thread.sleep, objct.wait,Thread.join,LockSupport.parkNanos,LockSupport.parkUntil。不分配CPU时间片,到时自行运行

TERMINATED:进程结束状态。

Java中线程状态转换图 转自 https://blog.csdn.net/pange1991/article/details/53860651 关于Java线程状态转化,这个博主说的挺好
在这里插入图片描述

发布了27 篇原创文章 · 获赞 1 · 访问量 674

猜你喜欢

转载自blog.csdn.net/SUKI547/article/details/103388713