用於與Java運行時環境交互的類在Java中稱為運行時類,它提供了Runtime.getRuntime()、exit(int status)、addShutdownHook(Thread hook)、Process exec( String命令)拋出輸入輸出異常,availableProcessors()、freeMemory()、totalMemory()等,用於進程執行、調用GC、獲取總內存、獲取空閒內存等,聲明為public class Runtime 擴展了對象,並且只有一個Java 應用程序只能使用java.lang.Runtime 類別的一個實例,並且使用Java 中的Runtime.getRuntime() 方法傳回Runtime 類別的單例實例。
Java運行時類別有多種方法。他們是:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
使用 Runtime getRuntime() 方法傳回 Run time 類別的實例。考慮下面的 Java 程式來示範 Java 執行時期類別的 Runtime getRuntime() 方法。
代碼:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { // The current runtime in association with process is obtained using getRuntime method. Runtime roll = Runtime.getRuntime(); // The current free memory for the current runtime is obtained using freeMemory() method. System.out.println("The current free memory for the current runtime is" + roll.freeMemory()); } }
輸出:
說明:在上面的程式中,定義了一個名為program的類別。然後呼叫main方法。然後使用 getRuntime 方法取得與進程關聯的目前運行時間。然後使用 freeMemory() 方法取得目前運行時的目前可用記憶體。
使用 Runtime exit(int status) 方法終止目前虛擬機器。考慮下面的 Java 程式來示範 Java 執行時期類別的 exit(int status) 方法。
代碼:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //the current runtime or program is caused to exit using exit(int status) method Runtime.getRuntime().exit(0); //This statement is not executed because the program is terminated by the usage of exit(int status) method above System.out.println("Checking if the program executes this statement depsite the use of exit(int status) method"); } }
輸出:
上述程式的輸出是空白的。
說明:在上面的程式中,定義了一個名為program的類別。然後呼叫main方法。然後使用 exit(int status) 方法導致目前執行時間或程式退出。那麼該語句就不會被執行,因為程式被上面語句中的 exit(int status) 方法終止了。
使用addShutdownHook(Thread Hook)方法註冊一個新的鉤子執行緒。考慮下面的 Java 程式來示範 Java 執行時期類別的 addShutdownHook(Thread Hook) 方法。
代碼:
//a class called program is defined public class program { // when the program is about to exit, this class extending the thread is called static class Mess extends Thread { public void run() { System.out.println("The program is going to exit"); } } //main method is called public static void main(String[] args) { try { //the class mess is registered as shut down hook Runtime.getRuntime().addShutdownHook(new Mess()); //The thread is made to sleep for certain seconds System.out.println("Five seconds of waiting time for the program to exit"); Thread.sleep(5); } catch (Exception ex) { ex.printStackTrace(); } } }
輸出:
說明:在上面的程式中,定義了一個名為program的類別。當程式即將退出時,會呼叫這個擴充執行緒的類別。然後呼叫main方法。然後類混亂被註冊為關閉鉤子。然後執行緒會休眠幾秒鐘。
給定的命令是使用 Process exec(String command) throws IOException 方法在單獨的進程中執行的。考慮下面的 Java 程式來示範 Process exec(String command) throws IOExceptionmethod of Java Run time class
代碼:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { try { // a process is created to execute google chrome Process proc = Runtime.getRuntime().exec("google-chrome"); System.out.println("Successfully started google chrome"); } catch (Exception e) { e.printStackTrace(); } } }
輸出:
說明:在上面的程式中,定義了一個名為program的類別。然後呼叫main方法。然後建立一個進程來執行google chrome。
可用處理器的數量使用 availableProcesors() 方法傳回。考慮下面的 Java 程式來示範 Java 執行時期類別的 availableProcessors() 方法。
代碼:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //checking for the number of available processors using availableprocessors() method. System.out.println("The number of available processors are " + Runtime.getRuntime().availableProcessors()); } }
輸出:
說明:在上面的程式中,定義了一個名為program的類別。然後呼叫main方法。然後使用 availableprocessors() 方法檢查可用處理器的數量。
使用 freeMemory() 方法在 JVM 中傳回可用記憶體量。
考慮下面的 Java 程式來示範 Java 執行時期類別的 freeMemory() 方法。
代碼:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //The amount of free memory available is given by using freememory() method System.out.println("The amount of free memory available is " + Runtime.getRuntime().freeMemory()); } }
輸出:
Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of free memory available is given by using freememory() method.
The amount of total memory is returned in JVM using totalMemory()method. Consider the below Java program to demonstrate totalMemory() method of the Java Run time class.
Code:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //The amount of total memory available is given by using totalmemory() method System.out.println("The amount of free memory available is " + Runtime.getRuntime().totalMemory()); } }
Output:
Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of total memory available is given by using totalmemory() method.
以上是Java運行時類的詳細內容。更多資訊請關注PHP中文網其他相關文章!