java - 在 Java 中启动进程后如何退出 while 循环?

标签 java process android-emulator kill-process

刚刚找到这篇文章(并找到了我也在下面粘贴的代码):

java runtime.getruntime() getting output from executing a command line program

我的问题是,如何终止该进程?看起来代码在 while 循环中阻塞。我尝试了几种选择,例如使用 boolean 值、在单独的线程中运行所有代码等等,但没有成功。 我只想启动一个 Android 模拟器并在需要时终止它。

Runtime rt = Runtime.getRuntime();
String[] commands = {"emulator", "-avd", "jenkins", 
                    "-scale", "96dpi", "-dpi-device", "100"};
Process proc = rt.exec(commands);

BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new 
     InputStreamReader(proc.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
    System.out.println(s);
}

最佳答案

好的。

使用下面的代码获取当前正在运行的线程或进程的进程 ID。

  String processName =java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
  String ProcessID = processName.split("@")[0];//Process Id

使用该进程 ID 来终止 CPU 中的该进程。

我认为为此目的,您可能希望在 While 循环中编写任何其他触发器或任何条件。

关于java - 在 Java 中启动进程后如何退出 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30319218/

相关文章:

java - 为什么 JsonHttpContent 的输出为空?

c# - 使用 Process 类检查控制台应用程序是否仍在运行

android - 在 Android 模拟器中访问安全站点

java - 在这种特定情况下如何使用 Streams 获取列表列表?

java - Resteasy 返回 403 未在 junit 中处理

java - Butterknife @generate 不工作{错误 :(23, 6) 错误:找不到符号类绑定(bind)}

java - JVM 进程与 JVM 堆内存使用情况

c++ - 构建进程控制守护进程 (PCD) 时出错

在模拟器中没有看到 Android 日历应用程序。

Android 虚拟设备管理器 - 4.4.2 上的 Webcam0 无法正常工作