运行外部进程的非阻塞线程 [英] Non-blocking thread that runs an external process

查看:150
本文介绍了运行外部进程的非阻塞线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Java GUI应用程序,它充当许多低级外部进程的包装器。该实用程序按原样工作,但迫切需要一个重大改进。

I have created a Java GUI application which functions as a wrapper for many low level external processes. The utility works as is, but is in desperate need of one major improvement.

我希望我的外部进程以非阻塞方式运行,这将允许我提供额外服务请求并行。简而言之,我希望能够在生成数据时处理来自外部流程的数据。但似乎我检查并查看外部进程是否仍在运行的基本尝试是阻塞。

I want my external process run in a non-blocking manner which would permit me to service additional requests in parallel. In a nutshell I want to be able to process data from the external process as the data is being generated. But it appears my basic attempt to check and see if the external process is still running is blocking.

以下是我的ExternalProcess类的摘录。有关线程和阻塞的特定Java功能问题,请参阅内联注释。

Below is an excerpt from my ExternalProcess class. Please see inline comments for specific Java functionality questions about threading and blocking.

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 I have
            // removed all file processing to eliminate it as the cause
            // of blocking.  THIS ROUTINE STILL BLOCKS!
            Thread.sleep(1000);
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    System.out.println("Exiting run method ...\n");  // Debug purposes only.
        // Show that we are exiting the run loop.
}

// Process (instantiated from Runtime.getRuntime().execute doesn't supports
// either fire-and-forget backgrounding (non-blocking) or you can wait for 
// the process to finish using the waitFor() method (blocking).  I want to
// be able to execute a non-blocking external process that I monitor via
// threading allowing me to process the external process file IO as it is
// created.  To facilitate this goal, I have created an isRunning() method
// that uses the exitValue() method.  If the process is still running, a 
// call to exitValue() will throw an IllegalThreadStateException exception.
// So I simply catch this execption to test if the background process is
// finished -- at which point I can stop processing file IO from the 
// process.  Is this the source of the blocking?  If so, is there another
// way to do this?
public boolean IsRunning()
{
    boolean isRunning = false;
    try
    {
        int exitVal = Process.exitValue();
    }
    catch(IllegalThreadStateException e)
    {
        isRunning = true;
    }
    return isRunning;
}


推荐答案

运行()方法线程实际上并不启动新线程,请尝试使用Thread.start()。

The run() method on Thread doesn't actually start a new thread, try using Thread.start() instead.

这篇关于运行外部进程的非阻塞线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆