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

如何在Java中启动进程后退出while循环?,java,process,android-emulator,kill-process,Java,Process,Android Emulator,Kill Process,刚找到这篇文章(找到了我正在粘贴的代码): 我的问题是,我如何终止这个过程?代码似乎在while循环中阻塞。我尝试过几种方法,比如使用布尔值、在单独的线程中运行所有代码等等,但都没有成功。 我只想启动一个Android仿真器,并随时杀死它 Runtime rt = Runtime.getRuntime(); String[] commands = {"emulator", "-avd", "jenkins", "-scale", "96dpi", "-

刚找到这篇文章(找到了我正在粘贴的代码):

我的问题是,我如何终止这个过程?代码似乎在while循环中阻塞。我尝试过几种方法,比如使用布尔值、在单独的线程中运行所有代码等等,但都没有成功。 我只想启动一个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循环中编写任何其他触发器或任何条件。

您想用另一个线程杀死实际上从控制台获得
ErrorStream的线程吗?我只想在不再需要它时停止模拟器。