简体   繁体   English

Java:为什么我的外部程序在Java中调用时会被冻结?

[英]Java: Why does my external program being run freeze when called within Java?

I am calling an external application from my Java GUI. 我从Java GUI调用外部应用程序。 The Java code is below when the user hits the "RUN" button in the GUI: 当用户点击GUI中的“RUN”按钮时,Java代码如下:

Runtime runme = Runtime.getRuntime();
runme.exec("MyApp.bin");

MyApp.bin does some math calculations and has some loops in it - no big deal. MyApp.bin做了一些数学计算并且有一些循环 - 没什么大不了的。 What happens is that MyApp.bin gets stuck! 会发生什么是MyApp.bin卡住了! When I close my Java GUI, then MyApp.bin continues to run and finishes. 当我关闭我的Java GUI时,MyApp.bin继续运行并完成。 If I run MyApp.bin directly from the terminal, then it runs fine without freezing. 如果我直接从终端运行MyApp.bin,那么它运行正常而不会冻结。 Why does my application freeze when it is run from the Java GUI, but resumes when I close the Java GUI? 为什么我的应用程序在从Java GUI运行时会冻结,但在关闭Java GUI时会恢复? What is the Java GUI or Java code doing that is blocking my application from running successfully? 什么是Java GUI或Java代码阻止我的应用程序成功运行?

I'm going to make a wild guess that MyApp.bin is outputting something to its standard out, and you're not reading it. 我将疯狂地猜测MyApp.bin正在输出符合标准的东西,而你却没有阅读它。 This causes the buffer to fill, and blocks your process. 这会导致缓冲区填充,并阻止您的进程。

Runtime.exec() returns a Process object. Runtime.exec()返回一个Process对象。 If you read the javadoc for that you'll find: 如果您阅读了javadoc,您会发现:

The created subprocess does not have its own terminal or console. 创建的子进程没有自己的终端或控制台。 All its standard io (ie stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). 它的所有标准io(即stdin,stdout,stderr)操作将通过三个流(getOutputStream(),getInputStream(),getErrorStream())重定向到父进程。 The parent process uses these streams to feed input to and get output from the subprocess. 父进程使用这些流向子进程提供输入并从子进程获取输出。 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. 由于某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,因此无法及时写入输入流或读取子进程的输出流可能导致子进程阻塞甚至死锁。

http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM