前面谈到了用Process类调用外部程序的问题时,说到了Runtime,所以今天我就看一点相关的东西。
Every Java application has a single instance of class Runtime
that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime
method.
An application cannot create its own instance of this class.
基本上可以使用这个类获得一些processer,memory相关的信息。
其他的应用是:
1, 执行一个新进程
比如执行外部程序时用的语句是:
Runtime.getRuntime().exec(command, getEnvp(), getWorkingFolder());
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Given an array of strings cmdarray
, representing the tokens of a command line, and an array of strings envp
, representing "environment" variable settings, this method creates a new process in which to execute the specified command.
好多时候String[] evn不需要什么,传null就可以了。
2,ShutdownHook
还有一个addShutdownHook方法的使用,我在http://blog.csdn.net/onlyqi/article/details/6556748 中提到的。
static class ShutdownHook extends Thread
{
public void run() { unlockFile();
}
ShutdownHook shutdownHook = new ShutdownHook();
Runtime.getRuntime().addShutdownHook(shutdownHook);
A shutdown hook is simply an initialized but unstarted thread.
When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently.
When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled.
Finally, the virtual machine will halt.
Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit
method.
3, trace 一些对象。目前不太懂。
4, Run finalization methods.