Skip to content

Commit 06332cd

Browse files
Update b11_RuntimeExample.java
1 parent 4946707 commit 06332cd

1 file changed

Lines changed: 71 additions & 52 deletions

File tree

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,95 @@
1-
package a1_java.lang;
2-
import java.io.*;
3-
import java.util.Map;
4-
public class b11_RuntimeExample {
5-
@SuppressWarnings({ "removal", "deprecation" })
6-
public static void main(String[] args) {
1+
package a1_java.lang;
2+
3+
import java.io.*;
4+
import java.util.Map;
5+
6+
public class b11_RuntimeExample {
7+
8+
@SuppressWarnings({"removal", "deprecation"})
9+
public static void main(String[] args) {
710
// Method 1: Get Runtime Instance
811
// This method returns the current runtime object, allowing access to JVM runtime methods
9-
Runtime runtime = Runtime.getRuntime();
10-
System.out.println("1. Runtime Instance obtained successfully.");
12+
Runtime runtime = Runtime.getRuntime();
13+
System.out.println("1. Runtime Instance obtained successfully.");
14+
1115
// Method 2 & 3: Memory Management Methods
1216
// These methods provide information about JVM memory usage
13-
long totalMemory = runtime.totalMemory();
14-
long freeMemory = runtime.freeMemory();
15-
System.out.println("2. Total Memory: " + totalMemory + " bytes");
16-
System.out.println("3. Free Memory: " + freeMemory + " bytes");
17+
long totalMemory = runtime.totalMemory();
18+
long freeMemory = runtime.freeMemory();
19+
System.out.println("2. Total Memory: " + totalMemory + " bytes");
20+
System.out.println("3. Free Memory: " + freeMemory + " bytes");
21+
1722
// Method 4: Garbage Collection
1823
// Suggests the JVM to run garbage collection process
19-
runtime.gc();
20-
System.out.println("4. Garbage Collection suggested.");
24+
runtime.gc();
25+
System.out.println("4. Garbage Collection suggested.");
26+
2127
// Method 5: Run Finalization
2228
// Suggests running pending finalizer methods
23-
runtime.runFinalization();
24-
System.out.println("5. Finalization process suggested.");
29+
runtime.runFinalization();
30+
System.out.println("5. Finalization process suggested.");
31+
2532
// Method 7 & 8 & 9: Execute External Processes
26-
try {
33+
try {
2734
// Execute a simple command and read its output
28-
Process process1 = runtime.exec("echo Hello, Runtime!");
29-
String output1 = readProcessOutput(process1);
30-
System.out.println("7. Simple command execution result: " + output1);
35+
Process process1 = runtime.exec("echo Hello, Runtime!");
36+
String output1 = readProcessOutput(process1);
37+
System.out.println("7. Simple command execution result: " + output1);
38+
3139
// Execute a command array and read its output
32-
String[] cmdArray = {"cmd", "/c", "dir"};
33-
Process process2 = runtime.exec(cmdArray);
34-
String output2 = readProcessOutput(process2);
35-
System.out.println("8. Command array execution result length: " + output2.length());
40+
String[] cmdArray = {"cmd", "/c", "dir"};
41+
Process process2 = runtime.exec(cmdArray);
42+
String output2 = readProcessOutput(process2);
43+
System.out.println("8. Command array execution result length: " + output2.length());
44+
3645
// Execute command with environment variables and read its output
37-
String[] envp = {"PATH=" + System.getenv("PATH")};
38-
Process process3 = runtime.exec("echo %PATH%", envp);
39-
String output3 = readProcessOutput(process3);
40-
System.out.println("9. Command with environment variables execution result: " + output3);
41-
} catch (IOException e) {
42-
System.err.println("Error executing processes: " + e.getMessage()); }
46+
String[] envp = {"PATH=" + System.getenv("PATH")};
47+
Process process3 = runtime.exec("echo %PATH%", envp);
48+
String output3 = readProcessOutput(process3);
49+
System.out.println("9. Command with environment variables execution result: " + output3);
50+
} catch (IOException e) {
51+
System.err.println("Error executing processes: " + e.getMessage());
52+
}
53+
4354
// Method 10: Add Shutdown Hook
4455
// Registers a thread to be executed when JVM is shutting down
45-
Thread shutdownHook = new Thread(() -> {
46-
System.out.println("10. Shutdown hook executed: Performing cleanup."); });
47-
runtime.addShutdownHook(shutdownHook);
56+
Thread shutdownHook = new Thread(() -> {
57+
System.out.println("10. Shutdown hook executed: Performing cleanup.");
58+
});
59+
runtime.addShutdownHook(shutdownHook);
60+
4861
// Method 11: Remove Shutdown Hook (demonstration)
49-
boolean hookRemoved = runtime.removeShutdownHook(shutdownHook);
50-
System.out.println("11. Shutdown hook removal status: " + hookRemoved);
62+
boolean hookRemoved = runtime.removeShutdownHook(shutdownHook);
63+
System.out.println("11. Shutdown hook removal status: " + hookRemoved);
64+
5165
// Method 12: Get Environment Variables
5266
// Retrieves all environment variables
53-
Map<String, String> environmentVariables = System.getenv();
54-
System.out.println("12. Environment Variables:");
55-
environmentVariables.forEach((key, value) ->
56-
System.out.println(key + " = " + value) );
67+
Map<String, String> environmentVariables = System.getenv();
68+
System.out.println("12. Environment Variables:");
69+
environmentVariables.forEach((key, value) ->
70+
System.out.println(key + " = " + value));
71+
5772
// Method 13: toString Representation
5873
// Returns a string representation of the Runtime object
59-
String runtimeString = runtime.toString();
60-
System.out.println("13. Runtime toString: " + runtimeString);
74+
String runtimeString = runtime.toString();
75+
System.out.println("13. Runtime toString: " + runtimeString);
76+
6177
// Method 14: Available Processors
6278
// Returns the number of processors available to the JVM
63-
int processors = runtime.availableProcessors();
64-
System.out.println("14. Available Processors: " + processors); }
79+
int processors = runtime.availableProcessors();
80+
System.out.println("14. Available Processors: " + processors);
81+
}
82+
6583
// Utility method to read output from a Process
66-
private static String readProcessOutput(Process process) throws IOException {
84+
private static String readProcessOutput(Process process) throws IOException {
6785
// Read the output of the process
68-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
69-
StringBuilder output = new StringBuilder();
70-
String line;
71-
while ((line = reader.readLine()) != null) {
72-
output.append(line).append("\n"); }
73-
return output.toString().trim();
74-
}
75-
}
86+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
87+
StringBuilder output = new StringBuilder();
88+
String line;
89+
while ((line = reader.readLine()) != null) {
90+
output.append(line).append("\n");
91+
}
92+
return output.toString().trim();
93+
}
94+
}
7695
}

0 commit comments

Comments
 (0)