package java.lang;
import java.io.*;
import java.util.StringTokenizer;
/**
*
* 控制java虛擬機器的對象,它是一個單例
*
* comment by liqiang
*
*/
public class Runtime {
//類初始化時產生唯一的一個執行個體
private static Runtime currentRuntime = new Runtime();
//獲得Runtime的唯一執行個體
public static Runtime getRuntime() {
return currentRuntime;
}
//私人建構函式
private Runtime() {}
/**
*
* 終止當前啟動並執行java虛擬機器
*
* 它會按照終止順序終止JVM,順序包括兩個部分
* 1 運行所有註冊的關閉鉤的線程
* 2 如果設定狀態為關閉時做清除,則所有未清除的對象將調用其finalize方法
*
*/
public void exit(int status) {
//安全檢查
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExit(status);
}
//退出虛擬機器
Shutdown.exit(status);
}
/**
*
* 註冊一個關閉鉤
*
*/
public void addShutdownHook(Thread hook) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("shutdownHooks"));
}
Shutdown.add(hook);
}
/**
*
* 登出一個註冊鉤
*
*/
public boolean removeShutdownHook(Thread hook) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("shutdownHooks"));
}
return Shutdown.remove(hook);
}
/**
*
* 強制停止正在啟動並執行虛擬機器
* 它不會運行關閉鉤線程,也不會調用未清除的對象的finalize方法
*
*/
public void halt(int status) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkExit(status);
}
Shutdown.halt(status);
}
/**
*
* 在虛擬機器退出是調用未清除對象的finalize方法
* value為true表示調用,false表示不調用
*
*/
public static void runFinalizersOnExit(boolean value) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
try {
security.checkExit(0);
} catch (SecurityException e) {
throw new SecurityException("runFinalizersOnExit");
}
}
Shutdown.setRunFinalizersOnExit(value);
}
/*
*
* 執行命令的本地方法
*
*/
private native Process execInternal(String cmdarray[], String envp[], String path)
throws IOException;
/**
*
* 調用系統命令
* 例如:Runtime.getRuntime().exec("notepad");將啟動一個記事本
*
*/
public Process exec(String command) throws IOException {
return exec(command, null);
}
/**
*
* 調用系統命令
*
* @param cmd 系統的命令列命令
* @param envp 表示命令的參數,是name=value值對的形式
*
*/
public Process exec(String cmd, String envp[]) throws IOException {
return exec(cmd, envp, null);
}
/**
*
* 調用系統命令
*
*/
public Process exec(String command, String envp[], File dir)
throws IOException {
int count = 0;
//命令數組
String cmdarray[];
StringTokenizer st;
//命令字串的長度為0拋出異常
if (command.length() == 0)
throw new IllegalArgumentException("Empty command");
//拆分命令字串到一個數組中
st = new StringTokenizer(command);
count = st.countTokens();
cmdarray = new String[count];
//重建一個StringTokenizer是沒有必要的
st = new StringTokenizer(command);
count = 0;
while (st.hasMoreTokens()) {
cmdarray[count++] = st.nextToken();
}
return exec(cmdarray, envp, dir);
}
/**
*
* 調用系統命令
*
*/
public Process exec(String cmdarray[]) throws IOException {
return exec(cmdarray, null);
}
/**
*
* 調用系統命令
*
*/
public Process exec(String cmdarray[], String envp[]) throws IOException {
return exec(cmdarray, envp, null);
}
/**
*
* 調用系統命令
*
* @param cmdarray 命令數組
* @param envp 參數數組
* @param dir 子處理的工作路徑,如果為null則使用單前的工作路徑
*
*/
public Process exec(String cmdarray[], String envp[], File dir)
throws IOException {
//拷貝命令數組
cmdarray = (String[])cmdarray.clone();
//如果參數不為空白,則拷貝參數數組
envp = (envp != null ? (String[])envp.clone() : null);
//如果參數數組為空白數組則拋出異常
if (cmdarray.length == 0) {
throw new IndexOutOfBoundsException();
}
//檢查是否有null命令有則拋出異常
for (int i = 0; i < cmdarray.length; i++) {
if (cmdarray[i] == null) {
throw new NullPointerException();
}
}
//如果參數數組不為空白,檢查參數數組中是否有null項有則拋出異常
if (envp != null) {
for (int i = 0; i < envp.length; i++) {
if (envp[i] == null) {
throw new NullPointerException();
}
}
}
//安全檢查
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExec(cmdarray[0]);
}
String path = (dir == null ? null : dir.getPath());
//執行命令
return execInternal(cmdarray, envp, path);
}
/**
*
* 返回JVM的處理器數,最少為1
*
*/
public native int availableProcessors();
/**
*
* 返回可用的記憶體數
*
*/
public native long freeMemory();
/**
*
* 返回JVM佔用的記憶體數
*
* Returns the total amount of memory in the Java virtual machine.
* The value returned by this method may vary over time, depending on
* the host environment.
* <p>
* Note that the amount of memory required to hold an object of any
* given type may be implementation-dependent.
*
* @return the total amount of memory currently available for current
* and future objects, measured in bytes.
*/
public native long totalMemory();
/**
*
* 返回JVM可用的最大記憶體數,它的上限與實際實體記憶體有關,還和
* 當前作業系統的一個進程可使用的最大記憶體數有關
*
*/
public native long maxMemory();
/**
*
* 做記憶體回收處理
*
*/
public native void gc();
/* Wormhole for calling java.lang.ref.Finalizer.runFinalization */
private static native void runFinalization0();
/**
*
* 運行清空處理
*
*/
public void runFinalization() {
runFinalization0();
}
/**
*
* 設定是否顯示結構軌跡
*
*/
public native void traceInstructions(boolean on);
/**
*
* 設定方法調用軌跡
*
*/
public native void traceMethodCalls(boolean on);
/**
*
* 裝載指定檔案名稱的動態連結程式庫
* 例子: Runtime.getRuntime().load("/home/avh/lib/libX11.so");
*
*/
public void load(String filename) {
load0(System.getCallerClass(), filename);
}
//裝載動態連結程式庫
synchronized void load0(Class fromClass, String filename) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkLink(filename);
}
//如果不是絕對路徑則拋出異常
if (!(new File(filename).isAbsolute())) {
throw new UnsatisfiedLinkError(
"Expecting an absolute path of the library: " + filename);
}
ClassLoader.loadLibrary(fromClass, filename, true);
}
/**
*
* 載入動態連結程式庫
*
*/
public void loadLibrary(String libname) {
loadLibrary0(System.getCallerClass(), libname);
}
synchronized void loadLibrary0(Class fromClass, String libname) {
//安全檢查
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkLink(libname);
}
//如果存在File.separatorChar字元則拋出異常
if (libname.indexOf((int)File.separatorChar) != -1) {
throw new UnsatisfiedLinkError(
"Directory separator should not appear in library name: " + libname);
}
ClassLoader.loadLibrary(fromClass, libname, false);
}
public InputStream getLocalizedInputStream(InputStream in) {
return in;
}
public OutputStream getLocalizedOutputStream(OutputStream out) {
return out;
}
}