Java System類和Runtime類
System類
System類對讀者來說並不陌生,因為在之前所學知識中,需要列印結果時,使用的都是“System.out.println();”語句,這句程式碼中就使用了System類。System類定義了一些與系統相關的屬性和方法,它所提供的屬性和方法都是靜態的,因此,想要引用這些屬性和方法,直接使用System類呼叫即可。System類的常用方法如下表所示。
1.getProperties()方法
System類的getProperties()方法用於獲取當前系統的全部屬性,該方法會返回一個Properties物件,其中封裝了系統的所有屬性,這些屬性是以鍵值對形式存在的。
package cn.itcast.chapter05.example09;
import java.util.*;
/**
* System類的getProperties()方法
*/
public class Example09 {
public static void main(String[] args) {
// 獲取當前系統屬性
Properties properties = System.getProperties();
// 獲得所有系統屬性的key,返回Enumeration物件
Enumeration propertyNames = properties.propertyNames ();
while (propertyNames.hasMoreElements()) {
// 獲取系統屬性的鍵key
String key = (String) propertyNames.nextElement();
// 獲得當前鍵key對應的值value
String value = System.getProperty(key);
System.out.println(key + "--->" + value);
}
}
}
2.currentTimeMillis()
currentTimeMillis()方法返回一個long型別的值,該值表示當前時間與1970年1月1日0點0分0秒之間的時間差,單位是毫秒,通常也將該值稱作時間戳。
package cn.itcast.chapter05.example10;
/**
* 計算程式在進行求和操作時所消耗的時間
*/
public class Example10 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();// 迴圈開始時的當前時間
int sum = 0;
for (int i = 0; i < 100000000; i++) {
sum += i;
}
long endTime = System.currentTimeMillis();// 迴圈結束後的當前時間
System.out.println("程式執行的時間為:" + (endTime - startTime) + "毫秒");
}
}
3.arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
arraycopy()方法用於將一個數組中的元素快速拷貝到另一個數組。其中的引數具體作用如下:
- src:表示源陣列。
- dest:表示目標陣列。
- srcPos:表示源陣列中拷貝元素的起始位置。
- destPos:表示拷貝到目標陣列的起始位置。
- length:表示拷貝元素的個數。
package cn.itcast.chapter05.example11;
/**
* 陣列元素的拷貝
*/
public class Example11 {
public static void main(String[] args) {
int[] fromArray = { 101, 102, 103, 104, 105, 106 }; // 源陣列
int[] toArray = { 201, 202, 203, 204, 205, 206, 207 }; // 目標陣列
System.arraycopy(fromArray, 2, toArray, 3, 4); // 拷貝陣列元素
// 列印目標陣列中的元素
for (int i = 0; i < toArray.length; i++) {
System.out.println(i + ": " + toArray[i]);
}
}
}
4.SystemClock.uptimeMillis()
Runtime類
Runtime類用於表示虛擬機器執行時的狀態,它用於封裝JVM虛擬機器程序。每次使用java命令啟動虛擬機器都對應一個Runtime例項,並且只有一個例項,因此該類採用單例模式進行設計,物件不可以直接例項化。若想在程式中獲得一個Runtime例項,只能通過以下方式:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-mAMQIgEV-1610115213663)(img/1500706110171.png)]
案例程式碼
由於Runtime類封裝了虛擬機器程序,因此,在程式中通常會通過該類的例項物件來獲取當前虛擬機器的相關資訊。
package cn.itcast.chapter05.example12;
/**
* Runtime類的使用
*/
public class Example12 {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime(); // 獲取
System.out.println("處理器的個數: " + rt.availableProcessors() + "個");
System.out.println("空閒記憶體數量: " + rt.freeMemory() / 1024 / 1024 + "M");
System.out.println("最大可用記憶體數量: " + rt.maxMemory() / 1024 / 1024 + "M");
}
}
案例程式碼
Runtime類中提供了一個exec()方法,該方法用於執行一個dos命令,從而實現和在命令列視窗中輸入dos命令同樣的效果。例如,通過執行“notepad.exe”命令開啟一個Windows自帶的記事本程式
package cn.itcast.chapter05.example13;
import java.io.IOException;
/**
* 使用exec()方法開啟記事本
*/
public class Example13 {
public static void main(String[] args) throws IOException {
Runtime rt = Runtime.getRuntime(); // 建立Runtime例項物件
rt.exec("notepad.exe"); // 呼叫exec()方法
}
}
開啟記事本並在3秒後自動關閉
package cn.itcast.chapter05.example14;
/**
* 開啟的記事本並在3秒後自動關閉
*/
public class Example14 {
public static void main(String[] args) throws Exception {
Runtime rt = Runtime.getRuntime(); // 建立一個Runtime例項物件
Process process = rt.exec("notepad.exe");// 得到表示程序的Process物件
Thread.sleep(3000); // 程式休眠3秒
process.destroy(); // 殺掉程序
}
}
Runtime.getRuntime().availableProcessors(); // 獲取CPU核心數
Runtime.getRuntime().maxMemory(); // 獲取應用被分配的最大記憶體