Record Class ProcessInfo

java.lang.Object
java.lang.Record
cloud.opencode.base.core.process.ProcessInfo
Record Components:
pid - the process ID - 进程 ID
command - the executable name (may be empty) - 可执行文件名(可能为空)
commandLine - the full command line (may be empty) - 完整命令行(可能为空)
user - the process owner (may be empty) - 进程所有者(可能为空)
startTime - when the process started, or null if unknown - 进程启动时间,未知时为 null
cpuDuration - total CPU time used, or null if unknown - 已使用的 CPU 总时间,未知时为 null
alive - whether the process is currently alive - 进程是否存活

public record ProcessInfo(long pid, String command, String commandLine, String user, Instant startTime, Duration cpuDuration, boolean alive) extends Record
ProcessInfo - Immutable snapshot of process information ProcessInfo - 进程信息的不可变快照

Captures key attributes of an operating system process at a point in time. Built from ProcessHandle via the from(ProcessHandle) factory method.

在某一时刻捕获操作系统进程的关键属性。 通过 from(ProcessHandle) 工厂方法从 ProcessHandle 构建。

Fields | 字段:

  • pid - Process ID - 进程 ID
  • command - Executable name only (may be empty) - 可执行文件名(可能为空)
  • commandLine - Full command line (may be empty for security) - 完整命令行(出于安全可能为空)
  • user - Process owner (may be empty) - 进程所有者(可能为空)
  • startTime - When process started (may be null) - 进程启动时间(可能为 null)
  • cpuDuration - Total CPU time used (may be null) - 已使用的 CPU 总时间(可能为 null)
  • alive - Whether process is currently alive - 进程是否存活

Thread Safety | 线程安全: This record is immutable and thread-safe. 此记录是不可变的,线程安全。

Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ProcessInfo

      public ProcessInfo(long pid, String command, String commandLine, String user, Instant startTime, Duration cpuDuration, boolean alive)
      Compact constructor that enforces non-null for String fields. 紧凑构造函数,确保字符串字段非 null。
  • Method Details

    • from

      public static ProcessInfo from(ProcessHandle handle)
      Creates a ProcessInfo from a ProcessHandle. 从 ProcessHandle 创建 ProcessInfo

      All optional fields from ProcessHandle.Info are handled gracefully with sensible defaults (empty strings, null for temporal values).

      ProcessHandle.Info 中的所有可选字段均以合理默认值优雅处理 (空字符串、时间值为 null)。

      Parameters:
      handle - the process handle - 进程句柄
      Returns:
      a new ProcessInfo snapshot - 新的 ProcessInfo 快照
      Throws:
      NullPointerException - if handle is null - 若 handle 为 null
    • fromCurrent

      public static ProcessInfo fromCurrent()
      Creates a ProcessInfo for the current JVM process. 为当前 JVM 进程创建 ProcessInfo
      Returns:
      process info for the current process - 当前进程的进程信息
    • startInstant

      public Optional<Instant> startInstant()
      Returns the start time as an Optional. 以 Optional 形式返回启动时间。
      Returns:
      optional start instant - 可选的启动时间
    • cpuTime

      public Optional<Duration> cpuTime()
      Returns the CPU time as an Optional. 以 Optional 形式返回 CPU 时间。
      Returns:
      optional CPU duration - 可选的 CPU 时间
    • uptimeMillis

      public long uptimeMillis()
      Returns the uptime in milliseconds since the process started, or -1 if unknown. 返回进程启动以来的毫秒数,未知时返回 -1
      Returns:
      uptime in milliseconds, or -1 if start time is unknown - 运行时间(毫秒),未知时为 -1
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • pid

      public long pid()
      Returns the value of the pid record component.
      Returns:
      the value of the pid record component
    • command

      public String command()
      Returns the value of the command record component.
      Returns:
      the value of the command record component
    • commandLine

      public String commandLine()
      Returns the value of the commandLine record component.
      Returns:
      the value of the commandLine record component
    • user

      public String user()
      Returns the value of the user record component.
      Returns:
      the value of the user record component
    • startTime

      public Instant startTime()
      Returns the value of the startTime record component.
      Returns:
      the value of the startTime record component
    • cpuDuration

      public Duration cpuDuration()
      Returns the value of the cpuDuration record component.
      Returns:
      the value of the cpuDuration record component
    • alive

      public boolean alive()
      Returns the value of the alive record component.
      Returns:
      the value of the alive record component