jdk Runtime类使用
一、得到系统内存的一些信息
@Test
public void runtimeInfo() {
Runtime runtime = Runtime.getRuntime();
int processors = runtime.availableProcessors();
long freeMemory = runtime.freeMemory();
long maxMemory = runtime.maxMemory();
long totalMemory = runtime.totalMemory();
// processors=4, freeMemory=165713400, maxMemory=2837446656, totalMemory=192937984
logger.debug("processors={}, freeMemory={}, maxMemory={}, totalMemory={}", processors, freeMemory, maxMemory, totalMemory);
}
二、得到系统的环境变量
@Test
public void dirRuntimeProcess() throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String string = null;
while ((string = bufferedReader.readLine()) != null) {
System.out.println(string); // D:\Java\jdk\jdk1.8.0_152
}
process.waitFor();
System.out.println("return: " + process.exitValue()); // return: 0
}
三、得到java的版本号,这个和上述的不一样
@Test
public void getJavaVersion() {
try {
Process process = Runtime.getRuntime().exec("javac -version");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line); // javac 1.8.0_152
process.waitFor();
System.out.println("Process exitValue: " + process.exitValue());
} catch (Throwable t) {
t.printStackTrace();
}
}
四、执行外部命令得到的结果
@Test
public void execProgramC() {
try {
Process process = Runtime.getRuntime().exec("C:/Users/76801/Desktop/huhx.exe");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line); // Hello World.
process.waitFor();
System.out.println("Process exitValue: " + process.exitValue());
} catch (Throwable t) {
t.printStackTrace();
}
}
huhx.c比较简单,就是打印一句话。
#include<stdio.h>
void main() {
printf("Hello World.");
}
五、使用Runtime类导出mysql脚本
@Test
public void execMysqldump() throws IOException, InterruptedException {
String execCommand = "cmd c/ D:/Java/mysqldump.exe -uhuhx -phuhx boot_learn > D:/bootlearn.sql";
System.out.println("exec command: " + execCommand);
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec(execCommand);
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
p.waitFor();
System.out.println("successful." + p.exitValue());
}
上述也使用到了网上所说的读出窗口的标准输出缓冲区中的内容,仍旧没有解决Process的waitFor阻塞问题。下面是清空缓冲区的线程代码:
public class StreamGobbler extends Thread {
InputStream is;
String type;
public StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try (InputStreamReader isr = new InputStreamReader(is);) {
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type.equals("Error")) {
System.out.println("Error :" + line);
} else {
System.out.println("Debug:" + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码的目标是导出mysql数据库的脚本。没有找到问题的解决方案,运行环境是win10,jdk1.8。
六、addShutdownHook函数
void java.lang.Runtime.addShutdownHook(Thread hook)
该方法用来在jvm中增加一个关闭的钩子。当程序正常退出,系统调用 System.exit方法或虚拟机被关闭时才会执行添加的shutdownHook线程。其中shutdownHook是一个已初始化但并不有启动的线程,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以可通过这些钩子在jvm关闭的时候进行内存清理、资源回收等工作
public class TestRuntimeShutdownHook {
public static void main(String[] args) {
Thread shutdownHookOne = new Thread() {
public void run() {
System.out.println("shutdownHook one...");
}
};
Runtime.getRuntime().addShutdownHook(shutdownHookOne);
Runnable threadOne = new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread one doing something...");
}
};
Runnable threadTwo = new Thread() {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread two doing something...");
}
};
threadOne.run();
threadTwo.run();
}
}
thread one doing something... thread two doing something... shutdownHook one...