# Io / Github / Biezhi / Java11 / Processor

## Overview

The `io.github.biezhi.java11.processor` module demonstrates the **Process API improvements** introduced in Java 9 and further refined in Java 11. This module provides a working example of how to manage and inspect OS-level processes using the new `java.lang.ProcessHandle` and `java.lang.ProcessHandle.Info` APIs. It exists as part of a larger Java 11 feature showcase collection, giving engineers a quick reference for interacting with system processes programmatically.

## Key Classes

### Example

**Source:** [Example.java](src/main/java/io/github/biezhi/java11/processor/Example.java)

The `Example` class is a single-purpose demonstration of the Process API. It shows how to obtain a reference to the current JVM process and retrieve its operating system process ID (PID).

#### Class Purpose

This class illustrates the modern replacement for the older `java.lang.Runtime.exec()` approach to process management. Instead of dealing with `ProcessBuilder` and `Process` objects directly, Java 9 introduced `ProcessHandle` as a cleaner, more feature-rich API for process inspection and control.

#### Methods

##### `main(String[] args)`

- **Signature:** `public static void main(String[] args) : void`
- **Lines:** 19–22
- **Purpose:** Entry point that demonstrates getting the current process PID.
- **Behavior:**
  1. Calls `ProcessHandle.current()` to obtain a `ProcessHandle` representing the currently running JVM process.
  2. Invokes `pid()` on that handle to retrieve the native OS process identifier.
  3. Prints the PID to stdout.

```java
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("当前进程的 PID = " + currentProcess.pid());
```

#### Design Notes

- This is a demonstration class, not a production utility. It is intentionally minimal to keep the example easy to understand.
- The comment in the source notes that "JDK secretly tweaked the API again in Java 11" — the code shown is the version known to work on Java 11.

## How It Works

### The ProcessHandle API

The module centers on a single class from the `java.lang` package: `ProcessHandle`. This interface provides a handle to a native OS process and offers the following capabilities:

| Feature | Description |
|---------|-------------|
| **PID Access** | `pid()` returns the native process identifier |
| **Process Inspection** | `info()` returns a `ProcessHandle.Info` with command line, arguments, start time, etc. |
| **Process Traversal** | `children()`, `parents()`, `descendants()` enable walking the process tree |
| **Lifecycle** | `isAlive()`, `onExit()` (async), `destroy()`, `destroyForcibly()` for process termination |

### Flow: Getting the Current Process PID

```
Step 1: ProcessHandle.current()
        │
        ▼
Step 2: Obtain ProcessHandle (represents the current JVM)
        │
        ▼
Step 3: ProcessHandle.pid()
        │
        ▼
Step 4: Return the native OS PID as a long
```

The `ProcessHandle.current()` call is a static factory method that returns an optional-typed handle for the process in which the code is executing. Even though the return type is `ProcessHandle` directly (not `Optional`), the handle may represent a process that has already exited.

## Module Relationships

```mermaid
flowchart LR
  Example["Example"] -->|"uses"| ProcessHandle["ProcessHandle API"]
  ProcessHandle -->|"provides"| GetCurrent["current()"]
  GetCurrent -->|"returns"| ProcessHandleObj["ProcessHandle"]
  ProcessHandleObj -->|"has method"| GetPID["pid()"]
```

### Dependencies

This module uses only the standard Java SE library — specifically the `java.lang.ProcessHandle` and `java.lang.ProcessHandle.Info` interfaces introduced in JDK 9. There are no external dependencies.

## Notes for Developers

- **Java 9+ Requirement:** The `ProcessHandle` API requires at least Java 9. This module is written for Java 11.
- **ProcessHandle.Info is Optional:** Calling `info()` on a `ProcessHandle` returns an `Optional<ProcessHandle.Info>` because the information may not be available for all processes (e.g., processes owned by other users may be hidden by the OS).
- **Security Considerations:** Destroying or querying other processes may be subject to OS-level permissions. The API does not throw on denied access — it simply returns empty or `false`.
- **ProcessHandle vs Process:** The older `java.lang.Process` class from `Runtime.exec()` and `ProcessBuilder` still exists for backward compatibility. `ProcessHandle` is the preferred modern API. You can convert a `Process` to a `ProcessHandle` using `Process.toHandle()`.
- **No Child Modules:** This is a leaf-level demonstration package with no sub-packages.
