Java 17 Recipes


-12. Easily Retrieving Information on


Download 3.2 Mb.
Pdf ko'rish
bet43/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   39   40   41   42   43   44   45   46   ...   245
Bog'liq
Java 17 Recipes

2-12. Easily Retrieving Information on 
OS Processes
 Problem
You want the ability to find information regarding operating system processes.
 Solution
Use the updated Process API. The new ProcessHandle interface lets you easily obtain 
information on operating system processes. In the following code, all operating system 
processes are listed and printed to the terminal window.
public static void listProcesses(){
ProcessHandle.allProcesses()
Chapter 2 enhanCements from Java 9 through Java 17 


69
.forEach(System.out::println);
}
However, this is not very helpful, as it simply lists the process number of each 
operating system, which is not very useful. To obtain more detail on the process, we 
need to obtain the ProcessHandle and call on its helper methods, which is quite easy. 
The following code print much more information regarding each process, as it prints the 
ProcessHandle.Info itself.
public static void detailListProcesses(){
ProcessHandle.allProcesses()
.forEach(h->System.out.println(formattedProcess(h)));
}
public static String formattedProcess(ProcessHandle handle){
long pid = handle.pid();
boolean alive = handle.isAlive();
Optional cpuDuration = handle.info().totalCpuDuration();
Optional handleName = handle.info().command();
return pid + " " + alive + " " + handleName + ":"+ cpuDuration;
}
The following is the main class.
public static void main (String[] args){
listProcesses();
detailListProcesses();
detailListProcessUsers();
}
Sample output may look as follows.
3216 true Optional[C:\Program Files
(x86)\Google\Chrome\Application\chrome.exe]:Optional[PT0.15625S]
2588 false Optional.empty:Optional.empty
6636 false Optional.empty:Optional.empty
10140 false Optional.empty:Optional.empty
8776 true Optional[C:\Program Files
(x86)\Notepad++\notepad++.exe]:Optional[PT2.859375S]
Chapter 2 enhanCements from Java 9 through Java 17 


70
11128 true Optional[C:\Program Files
(x86)\Google\Chrome\Application\chrome.exe]:Optional[PT0.0625S]
14184 true Optional[C:\Program Files
(x86)\Google\Chrome\Application\chrome.exe]:Optional[PT0.109375S]
14316 true Optional[C:\Windows\System32\SearchProtocolHost.exe]: 
Optional[PT0.0625S]
11356 false Optional.empty:Optional.empty
1468 true Optional[C:\jdk-17\bin\javaw.exe]:Optional[PT0.25S]
If you wish to retrieve information pertaining to the user that is running the process
that is easy to do as well.
public static void detailListProcessUsers(){
ProcessHandle.allProcesses()
.forEach(h->System.out.println(listOsUser(h)));
}
public static String listOsUser(ProcessHandle handle){
ProcessHandle.Info procInfo = handle.info();
return handle.pid() + ": " +procInfo.user();
}
Sample output using this technique may look as follows.
412: Optional.empty
14444: Optional[LAPTOP-BDD02I2D\lucky]
11648: Optional[LAPTOP-BDD02I2D\lucky]
7012: Optional[LAPTOP-BDD02I2D\lucky]
12908: Optional[LAPTOP-BDD02I2D\lucky]
10408: Optional[LAPTOP-BDD02I2D\lucky]
7984: Optional[LAPTOP-BDD02I2D\lucky]
1528: Optional[LAPTOP-BDD02I2D\lucky]
 How It Works
The ProcessHandle interface is introduced in Java 9, making the retrieval of operating 
system process information a first-class citizen of the JDK.
Chapter 2 enhanCements from Java 9 through Java 17 


71

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   39   40   41   42   43   44   45   46   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling