我创建了一个Java GUI应用程序,它可以作为许多低级外部进程的包装器.该实用程序按原样工作,但迫切需要一个重大改进.
我希望我的外部进程以非阻塞方式运行,这允许我并行地为其他请求提供服务.简而言之,我希望能够在生成数据时处理来自外部流程的数据.但似乎我检查并查看外部进程是否仍在运行的基本尝试是阻塞.
以下是我的ExternalProcess类的摘录.有关线程和阻止的特定Java功能问题,请参阅内联注释.
public void Execute()
{
System.out.println("Starting thread ...\n");
Runner = new Thread(this, "ExternalProcessTest");
Runner.run();
System.out.println("Ending thread ...\n");
}
public void run()
{
System.out.println("In run method ...\n"); // Debug purposes only.
// Show that we are in the run loop.
try
{
// Execute string command SomeCommand as background process ...
Process = Runtime.getRuntime().exec(SomeCommand);
while(IsRunning())
{
// External process generates file IO. I want to process these
// files inside this loop. For the purpose of this demo …Run Code Online (Sandbox Code Playgroud)