public class Runtime 
 extends Object 
| java.lang.Object | |
| ↳ | java.lang.Runtime | 
每个Java应用程序都有一个类Runtime的单个实例,允许应用程序与运行应用程序的环境进行交互。 当前的运行时间可以从getRuntime方法获得。
应用程序不能创建它自己的这个类的实例。
也可以看看:
| Public methods | |
|---|---|
|  void |  addShutdownHook(Thread hook) 注册一个新的虚拟机关机挂钩。 | 
|  int |  availableProcessors() 返回可用于Java虚拟机的处理器数量。 | 
|  Process |  exec(String[] cmdarray) 在单独的进程中执行指定的命令和参数。 | 
|  Process |  exec(String[] cmdarray, String[] envp) 使用指定的环境在单独的进程中执行指定的命令和参数。 | 
|  Process |  exec(String command) 在单独的进程中执行指定的字符串命令。 | 
|  Process |  exec(String command, String[] envp) 使用指定的环境在单独的进程中执行指定的字符串命令。 | 
|  Process |  exec(String[] cmdarray, String[] envp, File dir) 使用指定的环境和工作目录在单独的进程中执行指定的命令和参数。 | 
|  Process |  exec(String command, String[] envp, File dir) 使用指定的环境和工作目录在单独的进程中执行指定的字符串命令。 | 
|  void |  exit(int status) 通过启动其关闭序列来终止当前正在运行的Java虚拟机。 | 
|  long |  freeMemory() 返回Java虚拟机中的可用内存量。 | 
|  void |  gc() 运行垃圾回收器。 | 
|  InputStream |  getLocalizedInputStream(InputStream in) 在API级别1中弃用此方法。从JDK 1.1开始,将本地编码中的字节流转换为Unicode中的字符流的首选方法是通过 | 
|  OutputStream |  getLocalizedOutputStream(OutputStream out) 此方法在API级别1被废弃从JDK 1.1的,将Unicode字符流转换为本地编码字节流的首选方法是通过 | 
|  static Runtime |  getRuntime() 返回与当前Java应用程序关联的运行时对象。 | 
|  void |  halt(int status) 强制终止当前运行的Java虚拟机。 | 
|  void |  load(String filename) 将指定的文件名加载为动态库。 | 
|  void |  loadLibrary(String libname) 使用指定的库名加载动态库。 | 
|  long |  maxMemory() 返回Java虚拟机尝试使用的最大内存量。 | 
|  boolean |  removeShutdownHook(Thread hook) 取消注册先前注册的虚拟机关闭挂钩。 | 
|  void |  runFinalization() 运行任何挂起的对象的终止方法。 | 
|  static void |  runFinalizersOnExit(boolean value) 此方法在API级别1中已弃用。此方法本质上不安全。 这可能会导致终结器在活动对象上被调用,而其他线程同时操作这些对象,从而导致不稳定的行为或死锁。 | 
|  long |  totalMemory() 返回Java虚拟机中的总内存量。 | 
|  void |  traceInstructions(boolean enable) 启用/禁用跟踪指令。 | 
|  void |  traceMethodCalls(boolean enable) 启用/禁用方法调用的跟踪。 | 
| Inherited methods | |
|---|---|
|  From class  java.lang.Object  | |
void addShutdownHook (Thread hook)
注册一个新的虚拟机关机挂钩。
Java虚拟机响应两种事件而 关闭 :
exit (equivalently, System.exit) method is invoked, or 关闭钩子只是一个初始化但未启动的线程。 当虚拟机开始其关闭序列时,它将以某种未指定的顺序启动所有已注册的关闭挂钩,并让它们同时运行。 当所有的钩子都完成后,它将会运行所有未被唤醒的终结器,如果已启用终止退出。 最后,虚拟机将停止。 请注意,守护进程线程将在关闭序列期间继续运行,如果通过调用exit方法启动关闭,非守护线程也将继续运行。
一旦关闭序列开始,只有通过调用 halt方法才能停止该方法,强制终止虚拟机。
一旦关机序列开始,就不可能注册新的关机挂钩或取消注册先前注册的挂机。 试图执行这些操作中的任何一个都会导致IllegalStateException被抛出。
关机挂钩在虚拟机的生命周期中的一个微妙时间运行,因此应该用防守编码。 特别是,它们应该被写成是线程安全的并尽可能避免死锁。 他们也不应该盲目地依赖那些可能已经注册了自己的关闭钩子的服务,因此可能自己也处于关闭的过程中。 尝试使用其他基于线程的服务(例如AWT事件派发线程)可能导致死锁。
关机挂钩也应该尽快完成工作。 当一个程序调用exit时 ,期望虚拟机会立即关闭并退出。 当由于用户注销或系统关闭而导致虚拟机终止时,底层操作系统可能只允许关闭并退出的固定时间。 因此,尝试任何用户交互或在关闭钩子中执行长时间运行计算是不可取的。
通过调用线程的ThreadGroup对象的uncaughtException方法,未捕获的异常在关闭钩子中处理,就像在任何其他线程中一样 。 此方法的默认实现将异常的堆栈跟踪打印到err并终止该线程; 它不会导致虚拟机退出或暂停。
在极少数情况下,虚拟机可能会中止 ,即停止运行而不干净地关闭。 当虚拟机在外部终止时会发生这种情况,例如Unix上的SIGKILL信号或Microsoft Windows上的TerminateProcess呼叫。 如果本机方法出错,例如破坏内部数据结构或尝试访问不存在的内存,则虚拟机也可能中止。 如果虚拟机中止,则不能保证是否将运行任何关闭挂钩。
| Parameters | |
|---|---|
| hook | Thread: An initialized but unstartedThreadobject | 
| Throws | |
|---|---|
| IllegalArgumentException | If the specified hook has already been registered, or if it can be determined that the hook is already running or has already been run | 
| IllegalStateException | If the virtual machine is already in the process of shutting down | 
| SecurityException | If a security manager is present and it denies RuntimePermission("shutdownHooks") | 
int availableProcessors ()
返回可用于Java虚拟机的处理器数量。
在特定的虚拟机调用期间,该值可能会更改。 因此,对可用处理器数量敏感的应用程序应偶尔对此属性进行轮询并适当调整其资源使用情况。
| Returns | |
|---|---|
| int | the maximum number of processors available to the virtual machine; never smaller than one | 
Process exec (String[] cmdarray)
在单独的进程中执行指定的命令和参数。
这是一种方便的方法。 表单exec(cmdarray)的调用的行为方式与调用exec(cmdarray, null, null)完全相同 。
| Parameters | |
|---|---|
| cmdarray | String: array containing the command to call and its arguments. | 
| Returns | |
|---|---|
| Process | A new Processobject for managing the subprocess | 
| Throws | |
|---|---|
| SecurityException | If a security manager exists and its checkExecmethod doesn't allow creation of the subprocess | 
| IOException | If an I/O error occurs | 
| NullPointerException | If cmdarrayisnull, or one of the elements ofcmdarrayisnull | 
| IndexOutOfBoundsException | If cmdarrayis an empty array (has length0) | 
也可以看看:
Process exec (String[] cmdarray, String[] envp)
使用指定的环境在单独的进程中执行指定的命令和参数。
这是一种方便的方法。 表单exec(cmdarray, envp)的调用的行为方式与调用exec(cmdarray, envp, null)完全相同 。
| Parameters | |
|---|---|
| cmdarray | String: array containing the command to call and its arguments. | 
| envp | String: array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process. | 
| Returns | |
|---|---|
| Process | A new Processobject for managing the subprocess | 
| Throws | |
|---|---|
| SecurityException | If a security manager exists and its checkExecmethod doesn't allow creation of the subprocess | 
| IOException | If an I/O error occurs | 
| NullPointerException | If cmdarrayisnull, or one of the elements ofcmdarrayisnull, or one of the elements ofenvpisnull | 
| IndexOutOfBoundsException | If cmdarrayis an empty array (has length0) | 
也可以看看:
Process exec (String command)
在单独的进程中执行指定的字符串命令。
这是一种方便的方法。 表单exec(command)的调用的行为方式与调用exec(command, null, null)完全相同 。
| Parameters | |
|---|---|
| command | String: a specified system command. | 
| Returns | |
|---|---|
| Process | A new Processobject for managing the subprocess | 
| Throws | |
|---|---|
| SecurityException | If a security manager exists and its checkExecmethod doesn't allow creation of the subprocess | 
| IOException | If an I/O error occurs | 
| NullPointerException | If commandisnull | 
| IllegalArgumentException | If commandis empty | 
Process exec (String command, String[] envp)
使用指定的环境在单独的进程中执行指定的字符串命令。
这是一种方便的方法。 形式exec(command, envp)的调用行为完全相同的方式调用exec(command, envp, null)。
| Parameters | |
|---|---|
| command | String: a specified system command. | 
| envp | String: array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process. | 
| Returns | |
|---|---|
| Process | A new Processobject for managing the subprocess | 
| Throws | |
|---|---|
| SecurityException | If a security manager exists and its checkExecmethod doesn't allow creation of the subprocess | 
| IOException | If an I/O error occurs | 
| NullPointerException | If commandisnull, or one of the elements ofenvpisnull | 
| IllegalArgumentException | If commandis empty | 
Process exec (String[] cmdarray, String[] envp, File dir)
使用指定的环境和工作目录在单独的进程中执行指定的命令和参数。
给定的字符串数组 cmdarray ,表示命令行的令牌,和一个字符串数组 envp ,代表“环境”变量设置,此方法创建在其中执行指定的命令的新方法。
此方法检查cmdarray是否是有效的操作系统命令。 哪些命令是有效的取决于系统,但至少该命令必须是非空字符串的非空列表。
如果 envp是 null ,则子进程将继承当前进程的环境设置。
在某些操作系统上启动进程可能需要一组最小系统相关环境变量。 因此,子进程可能会继承超出指定环境中的其他环境变量设置。
start()现在是使用修改的环境启动进程的首选方式。
新子进程的工作目录由dir指定。 如果dir是null ,则子进程继承当前进程的当前工作目录。
如果存在安全管理器, checkExec将使用数组cmdarray的第一个组件作为其参数调用它的方法。 这可能会导致SecurityException被抛出。
启动操作系统进程是高度依赖于系统的。 许多可能出错的事情包括:
在这种情况下,将抛出异常。 异常的确切性质取决于系统,但它始终是IOException一个子类。
| Parameters | |
|---|---|
| cmdarray | String: array containing the command to call and its arguments. | 
| envp | String: array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process. | 
| dir | File: the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process. | 
| Returns | |
|---|---|
| Process | A new Processobject for managing the subprocess | 
| Throws | |
|---|---|
| SecurityException | If a security manager exists and its checkExecmethod doesn't allow creation of the subprocess | 
| IOException | If an I/O error occurs | 
| NullPointerException | If cmdarrayisnull, or one of the elements ofcmdarrayisnull, or one of the elements ofenvpisnull | 
| IndexOutOfBoundsException | If cmdarrayis an empty array (has length0) | 
也可以看看:
Process exec (String command, String[] envp, File dir)
使用指定的环境和工作目录在单独的进程中执行指定的字符串命令。
这是一种方便的方法。 形式exec(command, envp, dir)的调用行为以完全相同的方式调用exec(cmdarray, envp, dir),其中cmdarray是在所有的令牌的阵列command 。
更确切地说, command字符串使用由调用new 创建的StringTokenizer(command)StringTokenizer分解为令牌,而不需要进一步修改字符类别。 然后将令牌生成器生成的令牌以相同的顺序放置在新的字符串数组cmdarray中。
| Parameters | |
|---|---|
| command | String: a specified system command. | 
| envp | String: array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process. | 
| dir | File: the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process. | 
| Returns | |
|---|---|
| Process | A new Processobject for managing the subprocess | 
| Throws | |
|---|---|
| SecurityException | If a security manager exists and its checkExecmethod doesn't allow creation of the subprocess | 
| IOException | If an I/O error occurs | 
| NullPointerException | If commandisnull, or one of the elements ofenvpisnull | 
| IllegalArgumentException | If commandis empty | 
也可以看看:
void exit (int status)
通过启动其关闭序列来终止当前正在运行的Java虚拟机。 此方法从不正常返回。 这个论点充当了一个状态代码; 按照惯例,非零状态码表示异常终止。
虚拟机的关闭顺序由两个阶段组成。 在第一阶段,所有已注册的shutdown hooks (如果有的话)都以某种未指定的顺序启动,并允许同时运行,直至完成。 在第二阶段中,如果启用了finalization-on-exit则所有未引用的终结器都会运行。 一旦完成虚拟机halts 。
如果在虚拟机开始其关闭序列之后调用此方法,则如果正在运行关闭挂钩,则此方法将无限期地阻塞。 如果关闭钩子已经运行并且启用了退出结束,那么如果状态为非零,则此方法将使用给定的状态代码暂停虚拟机; 否则,它会无限期地阻止。
System.exit方法是调用此方法的常规方法。
| Parameters | |
|---|---|
| status | int: Termination status. By convention, a nonzero status code indicates abnormal termination. | 
| Throws | |
|---|---|
| SecurityException | If a security manager is present and its checkExitmethod does not permit exiting with the specified status | 
long freeMemory ()
返回Java虚拟机中的可用内存量。 调用gc方法可能会增加由freeMemory.返回的值
| Returns | |
|---|---|
| long | an approximation to the total amount of memory currently available for future allocated objects, measured in bytes. | 
void gc ()
运行垃圾回收器。 调用此方法表明,Java虚拟机花费大量的工作来回收未使用的对象,以便使其当前占用的内存可供快速重用。 当控制从方法调用返回时,虚拟机已尽最大努力回收所有丢弃的对象。
名称gc代表“垃圾回收器”。 即使未明确调用gc方法,虚拟机也会在单独的线程中根据需要自动执行此回收过程。
方法 gc()是调用这种方法的常规和方便的手段。
InputStream getLocalizedInputStream (InputStream in)
此方法在API级别1中已弃用。 
 从JDK 1.1开始,将本地编码中的字节流转换为Unicode字符流的首选方法是通过InputStreamReader和BufferedReader类。
创建输入流的本地化版本。 此方法采用InputStream并在所有方面返回InputStream等同于参数的所有方面,但它是本地化的:从本地字符集中的字符被从流中读取,它们将自动从本地字符集转换为Unicode。
如果参数已经是本地化的流,那么它可能会作为结果返回。
| Parameters | |
|---|---|
| in | InputStream: InputStream to localize | 
| Returns | |
|---|---|
| InputStream | a localized input stream | 
OutputStream getLocalizedOutputStream (OutputStream out)
此方法在API级别1中已弃用。 
 从JDK 1.1,将Unicode字符流转换为本地编码字节流的首选方法是通过OutputStreamWriter , BufferedWriter ,和PrintWriter类。
创建输出流的本地化版本。 此方法采用OutputStream并返回OutputStream等同于参数的所有方面,但它是本地化的:由于Unicode字符被写入流,它们会自动转换为本地字符集。
如果参数已经是本地化的流,那么它可能会作为结果返回。
| Parameters | |
|---|---|
| out | OutputStream: OutputStream to localize | 
| Returns | |
|---|---|
| OutputStream | a localized output stream | 
Runtime getRuntime ()
返回与当前Java应用程序关联的运行时对象。 类Runtime中的大多数方法都是实例方法,必须针对当前运行时对象调用。
| Returns | |
|---|---|
| Runtime | the Runtimeobject associated with the current Java application. | 
void halt (int status)
强制终止当前运行的Java虚拟机。 此方法从不正常返回。
应谨慎使用此方法。 与exit方法不同,此方法不会导致关闭挂钩启动,并且如果启用了exitization,则不会运行未引用的终结器。 如果关闭序列已经启动,则此方法不会等待任何正在运行的关闭挂钩或终结器完成其工作。
| Parameters | |
|---|---|
| status | int: Termination status. By convention, a nonzero status code indicates abnormal termination. If theexit(equivalently,System.exit) method has already been invoked then this status code will override the status code passed to that method. | 
| Throws | |
|---|---|
| SecurityException | If a security manager is present and its checkExitmethod does not permit an exit with the specified status | 
void load (String filename)
将指定的文件名加载为动态库。 filename参数必须是完整的路径名称(例如Runtime.getRuntime().load("/home/avh/lib/libX11.so"); )。
首先,如果有安全管理器, checkLink filename作为参数调用其checkLink方法。 这可能会导致安全异常。
这与方法 loadLibrary(String)类似,但它接受一个通用文件名作为参数,而不仅仅是一个库名,允许加载任何本地代码文件。
方法 load(String)是调用这种方法的传统和方便的手段。
| Parameters | |
|---|---|
| filename | String: the file to load. | 
| Throws | |
|---|---|
| SecurityException | if a security manager exists and its checkLinkmethod doesn't allow loading of the specified dynamic library | 
| UnsatisfiedLinkError | if the file does not exist. | 
| NullPointerException | if filenameisnull | 
void loadLibrary (String libname)
使用指定的库名加载动态库。 包含本地代码的文件是从本地文件系统从传统获取库文件的位置加载的。 这个过程的细节是依赖于实现的。 从库名到特定文件名的映射是以系统特定的方式完成的。
首先,如果有安全管理器, checkLink libname作为参数调用其checkLink方法。 这可能会导致安全异常。
方法loadLibrary(String)是调用这种方法的常规方便手段。 如果要在实现类中使用本地方法,则标准策略是将本机代码放入库文件(称为LibFile ),然后放入静态初始化程序:
 
      
 static { System.loadLibrary("LibFile"); }
  
      within the class declaration. When the class is loaded and initialized, the necessary native code implementation for the native methods will then be loaded as well. 
     如果使用相同的库名称多次调用此方法,则会忽略第二个和后续调用。
| Parameters | |
|---|---|
| libname | String: the name of the library. | 
| Throws | |
|---|---|
| SecurityException | if a security manager exists and its checkLinkmethod doesn't allow loading of the specified dynamic library | 
| UnsatisfiedLinkError | if the library does not exist. | 
| NullPointerException | if libnameisnull | 
long maxMemory ()
返回Java虚拟机尝试使用的最大内存量。 如果没有固有限制,则返回值MAX_VALUE 。
| Returns | |
|---|---|
| long | the maximum amount of memory that the virtual machine will attempt to use, measured in bytes | 
boolean removeShutdownHook (Thread hook)
取消注册先前注册的虚拟机关闭挂钩。
| Parameters | |
|---|---|
| hook | Thread: the hook to remove | 
| Returns | |
|---|---|
| boolean | true if the specified hook had previously been registered and was successfully de-registered, false otherwise. | 
| Throws | |
|---|---|
| IllegalStateException | If the virtual machine is already in the process of shutting down | 
| SecurityException | If a security manager is present and it denies RuntimePermission("shutdownHooks") | 
也可以看看:
void runFinalization ()
运行任何挂起的对象的终止方法。 调用此方法表明,Java虚拟机花费大量精力来运行已发现被丢弃但其尚未运行finalize方法的对象的finalize方法。 当控制从方法调用返回时,虚拟机已尽最大努力完成所有未完成的结果。
如果未显式调用 runFinalization方法,虚拟机将根据需要在单独的线程中自动执行最终确定过程。
方法 runFinalization()是调用这种方法的常规和方便的手段。
也可以看看:
void runFinalizersOnExit (boolean value)
此方法在API级别1中已弃用。 
 这种方法本质上是不安全的。 这可能会导致终结器在活动对象上被调用,而其他线程同时操作这些对象,从而导致不稳定的行为或死锁。
退出时启用或禁用终止; 这样做指定了所有具有尚未自动调用终结器的对象的终结器将在Java运行时退出之前运行。 默认情况下,退出时的终止被禁用。
如果存在安全管理器,则首先使用0作为其参数来调用其方法checkExit ,以确保允许退出。 这可能会导致SecurityException。
| Parameters | |
|---|---|
| value | boolean: true to enable finalization on exit, false to disable | 
| Throws | |
|---|---|
| SecurityException | if a security manager exists and its checkExitmethod doesn't allow the exit. | 
也可以看看:
long totalMemory ()
返回Java虚拟机中的总内存量。 此方法返回的值可能随时间而变化,具体取决于主机环境。
请注意,保存任何给定类型的对象所需的内存量可能取决于实现。
| Returns | |
|---|---|
| long | the total amount of memory currently available for current and future objects, measured in bytes. | 
void traceInstructions (boolean enable)
启用/禁用跟踪指令。 如果boolean参数为true ,则此方法建议Java虚拟机在虚拟机执行时为其中的每条指令发出调试信息。 这些信息的格式,以及它发出的文件或其他输出流取决于主机环境。 如果虚拟机不支持此功能,它可能会忽略此请求。 跟踪输出的目的地是依赖于系统的。
如果参数 boolean为 false ,则此方法会导致虚拟机停止执行正在执行的详细指令跟踪。
| Parameters | |
|---|---|
| enable | boolean:trueto enable instruction tracing;falseto disable this feature. | 
void traceMethodCalls (boolean enable)
启用/禁用方法调用的跟踪。 如果boolean参数为true ,则此方法建议Java虚拟机在调用虚拟机时为每个方法发出调试信息。 这些信息的格式,以及它发出的文件或其他输出流取决于主机环境。 如果虚拟机不支持此功能,它可能会忽略此请求。
使用参数false调用此方法表明虚拟机停止发出每个调用的调试信息。
| Parameters | |
|---|---|
| enable | boolean:trueto enable instruction tracing;falseto disable this feature. |