The Runtime class represents the running state of the Java virtual machine and encapsulates the Java virtual machine process. Each time the Java virtual Machine is started using the Java command, there is a Runtime instance, and only one instance, through which the application is connected to the Runtime environment. An application cannot create its own Runtime instance. To obtain a Runtime instance from an application, you can use the getRuntime() method to obtain the Runtime object associated with it as follows:
Runtime run = Runtime.getRuntime();
Because the Runtime class encapsulates the Java virtual machine process, information about the current virtual machine can be obtained from the instance object of the game agent. The next example demonstrates the use of the Runtime class, as shown in file 1.
public class Example12 { public static void main(String[] args) { Runtime rt = Runtime.getRuntime(); System.out.println(” number of processors: “+ rt.availableProcessors() + “); System.out.println(” freeMemory size: “+ rt.freeMemory() / 1024/1024 + “M”); Println (” Max available memory size: “+ rt.maxMemory() / 1024/1024 + “M”); }}
The running results are shown in Figure 1:
01
In file 1, run runtime.getruntime (); Method creates an instance object of Runtime and calls the object’s availableProcessors(), freeMemory(), and maxMemory() methods, respectively, to print information about the current virtual machine’s number of processors, freeMemory size, and maximum available memory size.
It is important to note that the printable results of this file may vary depending on the configuration and performance of each computer. In addition, both the free memory size and the maximum available memory size are measured in bytes, and the results of the program run in file 1 have been translated into megabytes (M). If relative electron has more intuitive understanding, it can also refer to its format as follows:
Game Agent: www.walajiao.com
The Runtime class provides an exec() method that executes a DOS command to achieve the same effect as typing a DOS command in a command line window. For example, you can open a Windows notepad by running the “notepad.exe” command. The program code is shown in file 2.
import java.io.IOException; public class Example13 { public static void main(String[] args) throws IOException { Runtime rt = Runtime.getRuntime(); // Create Runtime instance object rt.exec(“notepad.exe”); // call exec() method}}
In file 2, the exec() method of the Runtime object is called and the system command “notepad.exe” is passed to the method as an argument. After running the program, a Notepad opens on the desktop, as shown in Figure 2:
02
At this point, a new process notepad.exe is created in the Windows system and can be observed through the task manager, as shown in Figure 3.
03
The exec() method of the Runtime class returns a game agent www.walajiao.comProcess object representing a Process of the operating system, in this case notepad.exe. The Process object can be used to manage the new Process. To shut down the process, simply call the destroy() method.
Next, an example is used to realize the function of opening notepad and closing it automatically after 3 seconds, as shown in file 3.
Public class Example14 {public static void main(String[] args) throws Exception {// Create a Runtime instance object Runtime rt = Runtime.getRuntime(); Process = rt.exec(“notepad.exe”); Thread.sleep(3000); // Close process.destroy(); }}
In file 3, the open Notepad is closed by calling the destroy() method of the Process object. To highlight the demo, the Thread class’s static method sleep(Long Millis) was used to put the program to sleep for three seconds, so that when the program runs, you’ll see that the open Notepad closes automatically after three seconds.