
使用 `runtime.exec` 执行外部命令时,其返回的 `process` 对象所提供的输入/输出流(`getinputstream()`、`getoutputstream()`、`geterrorstream()`)必须被显式关闭。未能及时关闭这些流会导致系统资源泄露、子进程阻塞甚至死锁,严重影响应用程序的稳定性和性能。本文将详细阐述其原因并提供正确的处理方法。
在 Java 应用程序中,Runtime.exec() 方法提供了一种执行外部系统命令或程序的机制。当一个外部程序通过 Runtime.exec() 启动时,Java 虚拟机(JVM)会创建一个 Process 对象来代表这个新启动的子进程。这个 Process 对象不仅允许我们控制子进程(如等待其完成),还提供了访问子进程标准输入、标准输出和标准错误流的接口:
这些流是 Java 进程与子进程之间进行通信的桥梁。然而,对这些流的管理不当是常见的错误源,可能导致难以诊断的资源泄露和程序挂起问题。
理解为何必须关闭这些流,关键在于认识到它们不仅仅是简单的 Java 对象,更是底层操作系统资源(如管道、文件句柄)的抽象。
操作系统为每个进程可打开的文件句柄数量通常是有限制的。Process 对象关联的每个流都对应着一个或多个底层操作系统句柄。如果这些流在使用完毕后不被显式关闭,即使 Java 的垃圾回收器最终回收了 Process 对象,底层的操作系统句柄也可能不会立即释放。长期累积未关闭的句柄会导致文件句柄泄露,最终可能耗尽系统资源,使得后续的程序操作(如打开文件、创建套接字)失败,报告“Too many open files”错误。
立即学习“Java免费学习笔记(深入)”;
Oracle 官方文档明确指出:
“Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.” 这意味着,操作系统为子进程的标准输入/输出流提供的缓冲区大小是有限的。如果子进程产生了大量输出(stdout 或 stderr),而父进程(Java 应用程序)没有及时从 getInputStream() 或 getErrorStream() 读取这些输出,那么子进程的输出缓冲区可能会被填满。一旦缓冲区满,子进程将阻塞,等待父进程读取数据以清空缓冲区。类似地,如果父进程向 getOutputStream() 写入数据,而子进程没有及时读取,子进程的输入缓冲区也可能被填满,导致父进程阻塞。更复杂的情况是,如果父进程在等待子进程完成(通过 process.waitFor()),而子进程又在等待父进程读取其输出(或提供输入),就会发生经典的死锁,导致整个应用程序挂起。
Process 对象被垃圾回收并不意味着子进程会终止。
“The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously.” 子进程是独立于 Java 进程运行的。即使 Process 对象在 Java 堆中不再被引用,子进程仍可能继续执行。如果流未关闭,子进程的输出可能永远无法被消费,从而导致上述的阻塞和资源占用问题。正确关闭流是确保子进程能够顺利完成其任务并释放其资源的关键一步。
为了避免上述问题,必须在不再需要时显式关闭 Process 对象的所有相关流。
对于 Java 7 及更高版本,try-with-resources 语句是管理流资源最推荐的方式,因为它能确保资源在块执行完毕后自动关闭,无论是否发生异常。然而,Process 对象本身并非 AutoCloseable,但其返回的流是。因此,我们通常需要手动获取这些流,并将其包装在 try-with-resources 块中。
示例代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class ProcessStreamHandler {
public static void main(String[] args) {
// 假设要执行的命令,Windows下可以是 "cmd /c dir",Linux/macOS下可以是 "ls -l"
String[] command = {"ls", "-l"};
// String[] command = {"cmd", "/c", "dir"}; // For Windows
StringBuilder output = new StringBuilder();
StringBuilder errorOutput = new StringBuilder();
int exitCode = -1;
Process process = null;
ExecutorService executor = Executors.newFixedThreadPool(2); // 用于异步读取stdout和stderr
try {
// 1. 启动子进程
process = Runtime.getRuntime().exec(command);
// 2. 异步读取子进程的标准输出流
Future<String> stdoutFuture = executor.submit(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append(System.lineSeparator());
}
} catch (IOException e) {
System.err.println("Error reading stdout: " + e.getMessage());
}
return output.toString();
});
// 3. 异步读取子进程的标准错误流
Future<String> stderrFuture = executor.submit(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String line;
while ((line = reader.readLine()) != null) {
errorOutput.append(line).append(System.lineSeparator());
}
} catch (IOException e) {
System.err.println("Error reading stderr: " + e.getMessage());
}
return errorOutput.toString();
});
// 4. 如果需要向子进程写入数据,这里是示例
// try (OutputStream os = process.getOutputStream()) {
// os.write("input to subprocess".getBytes());
// os.flush();
// } catch (IOException e) {
// System.err.println("Error writing to stdin: " + e.getMessage());
// }
// 5. 等待子进程完成
exitCode = process.waitFor();
// 6. 获取异步读取的结果
stdoutFuture.get(5, TimeUnit.SECONDS); // 等待结果,设置超时
stderrFuture.get(5, TimeUnit.SECONDS);
} catch (IOException e) {
System.err.println("Error executing command: " + e.getMessage());
} catch (InterruptedException e) {
System.err.println("Process was interrupted: " + e.getMessage());
Thread.currentThread().interrupt(); // 重新设置中断标志
} catch (Exception e) { // Catch all other exceptions from Future.get()
System.err.println("Error getting stream output: " + e.getMessage());
} finally {
// 7. 确保关闭所有流和销毁进程
if (process != null) {
// 显式关闭流,尽管try-with-resources会处理,但这里是兜底
try {
process.getInputStream().close();
} catch (IOException e) { /* ignore */ }
try {
process.getOutputStream().close();
} catch (IOException e) { /* ignore */ }
try {
process.getErrorStream().close();
} catch (IOException e) { /* ignore */ }
// 销毁进程,确保其终止
process.destroy();
}
// 8. 关闭线程池
executor.shutdownNow();
try {
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
System.err.println("Executor did not terminate in time.");
}
} catch (InterruptedException e) {
System.err.println("Executor termination interrupted.");
Thread.currentThread().interrupt();
}
}
System.out.println("--- Command Output ---");
System.out.println(output.toString());
System.out.println("--- Error Output ---");
System.out.println(errorOutput.toString());
System.out.println("--- Exit Code ---");
System.out.println(exitCode);
}
}代码解析:
对于不支持 try-with-resources 的老版本 Java,必须在 finally 块中手动关闭所有流。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class ProcessStreamHandlerLegacy {
public static void main(String[] args) {
String[] command = {"ls", "-l"}; // For Linux/macOS
// String[] command = {"cmd", "/c", "dir"}; // For Windows
StringBuilder output = new StringBuilder();
StringBuilder errorOutput = new StringBuilder();
int exitCode = -1;
Process process = null;
BufferedReader stdoutReader = null;
BufferedReader stderrReader = null;
OutputStream stdinWriter = null;
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
process = Runtime.getRuntime().exec(command);
// 获取流
stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
stderrReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
stdinWriter = process.getOutputStream(); // 如果需要写入
// 异步读取标准输出
Future<String> stdoutFuture = executor.submit(() -> {
String line;
try {
while ((line = stdoutReader.readLine()) != null) {
output.append(line).append(System.lineSeparator());
}
} catch (IOException e) {
System.err.println("Error reading stdout: " + e.getMessage());
}
return output.toString();
});
// 异步读取标准错误
Future<String> stderrFuture = executor.submit(() -> {
String line;
try {
while ((line = stderrReader.readLine()) != null) {
errorOutput.append(line).append(System.lineSeparator());
}
} catch (IOException e) {
System.err.println("Error reading stderr: " + e.getMessage());
}
return errorOutput.toString();
});
// 等待子进程完成
exitCode = process.waitFor();
// 获取异步读取的结果
stdoutFuture.get(5, TimeUnit.SECONDS);
stderrFuture.get(5, TimeUnit.SECONDS);
} catch (IOException e) {
System.err.println("Error executing command: " + e.getMessage());
} catch (InterruptedException e) {
System.err.println("Process was interrupted: " + e.getMessage());
Thread.currentThread().interrupt();
} catch (Exception e) {
System.err.println("Error getting stream output: " + e.getMessage());
} finally {
// 确保关闭所有流
try {
if (stdoutReader != null) stdoutReader.close();
} catch (IOException e) { /* ignore */ }
try {
if (stderrReader != null) stderrReader.close();
} catch (IOException e) { /* ignore */ }
try {
if (stdinWriter != null) stdinWriter.close();
} catch (IOException e) { /* ignore */ }
// 销毁进程
if (process != null) {
process.destroy();
}
// 关闭线程池
executor.shutdownNow();
try {
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
System.err.println("Executor did not terminate in time.");
}
} catch (InterruptedException e) {
System.err.println("Executor termination interrupted.");
Thread.currentThread().interrupt();
}
}
System.out.println("--- Command Output ---");
System.out.println(output.toString());
System.out.println("--- Error Output ---");
System.out.println(errorOutput.toString());
System.out.println("--- Exit Code ---");
System.out.println(exitCode);
}
}Runtime.exec 返回的 Process 对象所关联的流是连接父子进程的关键。正确地管理和关闭这些流对于避免资源泄露、防止程序阻塞和死锁至关重要。始终遵循以下原则:
通过遵循这些最佳实践,可以有效地管理外部进程,确保 Java 应用程序的健壮性和稳定性。
以上就是Java Runtime.exec 返回的进程流:资源管理与最佳实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号