Process API Improvements
Java 9 ปรับปรุง Process API ให้จัดการและควบคุม OS processes ได้ดีขึ้น เพิ่มคลาสและ interface ใหม่
ข้อมูลที่ ProcessHandle ให้มา
- Native Process ID (PID)
- Accumulated CPU Time
- Parent Process
- Method to destroy process
- Process Descendants
- Start time, User, Command, Arguments
Interface หลัก
- ProcessHandle - ระบุและควบคุม native process
- ProcessHandle.Info - snapshot ข้อมูล process
ตัวอย่าง
public class ProcessTest {
public static void main(String args[]) {
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("PID: " + currentProcess.pid());
ProcessHandle.Info currentProcessInfo = currentProcess.info();
System.out.println("totalCpuDuration: " + currentProcessInfo.totalCpuDuration());
System.out.println("user: " + currentProcessInfo.user());
}
}
Output
PID: 6004 totalCpuDuration: Optional[PT0.421875S] user: Optional[Tutorialspoint\User]
สรุป
Process API ใน Java 9 ให้ความสามารถจัดการ process ข้าม platform ได้ครบครัน ไม่ต้องใช้ JNI หรือ external libraries