程序中怎么区分status和state

106 阅读2分钟

PS:禁止拷贝形式转载,转载请以URL形式

1 参考

程序代码中,怎么区分status和state?

2 区分

2.1 一句话

state的status

2.2 一张图

Pasted image 20231123164709.png

  • state: 状态,表示程序某一个阶段的状态,一个程序的生命周期由固定数量的状态构成,侧重于对程序生命周期的划分归类。
    1. 如java线程的生命周期由固定的6种状态构成`NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED
    2. 如vue组件的生命周期划分为固定的三个阶段创建、运行、销毁
  • status: 状况,表示程序某一个时间的状况,一个程序当前处于某个状态state下的实际活动描述,侧重于当前程序在这个时间下具体干了什么,也可以说是对当前程序处于某state的详细描述
    1. 如java Thread线程源码用int threadStatus保存当前状况,当需要知道当前state时使用toThreadState(int threadStatus)函数转换为当前处于什么状态
    2. 如vue 生命周期三个阶段(state)有不同的执行钩子函数(status),这些钩子函数(status)只会在隶属于不同的阶段(state)中执行

3 论证

3.1 Java11.Thread

参考Java11.Thread类源码
java.Thread 源码使用int threadStatus变量保存实际活动状况,当用户要知道线程处于什么state 状态时再使用VM.toThreadState(int threadStatus)转换。证明了其状况和状态之间从属关系

java.lang.Thread

/*  
 * Java thread status for tools, default indicates thread 'not yet started' 
 */
private volatile int threadStatus;

/*  
 * 省略...................................
 */

/**  
 * Returns the state of this thread. 
 * This method is designed for use in monitoring of the system state, 
 * not for synchronization control. 
 * 
 * @return this thread's state.  
 * @since 1.5  
 */
public State getState() {  
    // get current thread state  
    return jdk.internal.misc.VM.toThreadState(threadStatus);  
}

jdk.internal.misc.VM

/**  
 * Returns Thread.State for the given threadStatus */public static Thread.State toThreadState(int threadStatus) {  
    if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {  
        return RUNNABLE;  
    } else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {  
        return BLOCKED;  
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {  
        return WAITING;  
    } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {  
        return TIMED_WAITING;  
    } else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {  
        return TERMINATED;  
    } else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {  
        return NEW;  
    } else {  
        return RUNNABLE;  
    }  
}

3.2 一命令

linux 下执行:systemctl status mariadb
这个不太严谨没有源码佐证,但是现象可以论证status 只是对当前实际活动状况的描述。其在运行时status 描述为"Taking your SQL requests now..."

root@arch:~# systemctl status  mariadb
● mariadb.service - MariaDB 11.1.2 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
     Active: active (running) since Thu 2023-11-16 16:35:03 CST; 6 days ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 516 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 577 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recove>
    Process: 728 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
   Main PID: 642 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 9 (limit: 9494)
     Memory: 62.3M
        CPU: 43.975s
     CGroup: /system.slice/mariadb.service
             └─642 /usr/bin/mariadbd

Notice: journal has been rotated since unit was started, output may be incomplete.