Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 共享进程/线程_Java_Multithreading_Process_Processbuilder - Fatal编程技术网

Java 共享进程/线程

Java 共享进程/线程,java,multithreading,process,processbuilder,Java,Multithreading,Process,Processbuilder,我面临一个关于进程和线程的问题。我的设想是: 我的Java应用程序称之为“Starter App”,在命名的线程中使用ProcessBuilder启动另一个exe应用程序(Diagnosis.exe): Thread startGXThread = new Thread(new Runnable() { @Override public void run() { try { ... File gx

我面临一个关于进程和线程的问题。我的设想是:

  • 我的Java应用程序称之为“Starter App”,在命名的
    线程中使用
    ProcessBuilder
    启动另一个exe应用程序(Diagnosis.exe):

    Thread startGXThread = new Thread(new Runnable() {  
        @Override       
        public void run() {
            try {
                ...
                File gxExe = new File(pathToGX); // Path to Diagnosis.exe
                gxp = pb.start();
                gxp.waitFor();
    
            } catch (IOException e) {
                LOG.error("Can't start module");
                LOG.error(e.getMessage(), e);
            } catch (InterruptedException e) {
                LOG.debug("thread interrupted. Destroy process");
                LOG.debug(e.getMessage(), e);
                if (gxp != null) {
                    gxp.destroy();
                    LOG.debug("process exit value: " + gxp.exitValue());
                }
            }
        }
    }, "diag_thrd");
    
  • 之后,使用webapp启动jetty Web服务器(ServiceWebApp)

  • 启动chromium并在其关闭时收听“Starter App”
  • 一旦chromium关闭,“Starter应用程序”识别到这一点并停止jetty,同时终止startet application.Diagnosis.exe。这是通过以下方式实现的:

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            stopAsync();
        }
    });
    
    public static void stopAsync() {
        Thread diag = getThread("diag_thrd");
        if (diag != null) {
            diag.interrupt();
        }
        if (gxp != null) {
            gxp.destroy();
            LOG.debug("process exit value: " + gxp.exitValue());
        }
    }
    
问题: 我需要能够从webapp中停止startet Diagnosis.exe并再次启动它,同时在“Starter App”中停止后仍然能够销毁/关闭Diagnosis.exe。
我希望我能解释我的问题,并希望得到建议。

我认为有一个解决方案,但很难实施

您始终可以使用*nixapi,如这里的示例中所述的
ps kill#pid


但是您的Web服务器必须知道要查找哪个PID。我看到的实现这种东西的唯一选择是使用套接字或Web服务。因此,您需要跟踪Diagnosis.exe进程的当前pid,并在终止之前使用该Id。

基于Anands answer,我认为您需要在Diagnosis.exe和Starter应用程序之间使用某种形式的IPC,使用websockets或其他一些选项,以获得一些想法:


Web应用程序将向Starter应用程序发送重新启动Diagnosis.exe的请求,Starter应用程序将始终负责管理应用程序trio。

我正在使用windows。所以它可能不是
ps kill
。我可以将pid作为系统参数传递给web应用程序,但一旦web应用程序关闭,“starter应用程序”的引用将不再有效。是的。Windows将有不同的选项。此外,我不建议将PID作为系统参数发送,但应将其作为心跳或某些RMI/Webservice调用发送。因为存在Web服务器重新启动进程的可能性。因此,任何人重新启动Diagnostic.exe都会通知其他人要查找的新进程Id。因此,Starter应用程序会启动Diagnostic.exe,一个jetty服务器和Chromium?没错。“Starter应用程序”启动Diagnosis.exe、Jetty(使用已部署的Web应用程序)和Chromium(使用Web应用程序的url)。一旦Chromium(通过用户)关闭,“Starter App”意识到这一点,并关闭jetty和Diagnosis.exe。喜欢这个想法-webapp将向Starter App发送重新启动Diagnosis.exe的请求,Starter App将始终负责管理应用程序三人组。