简体   繁体   English

等待命令行线程完成并以正确的方式退出

[英]wait for commandline Thread to finish and exit in proper way

I´ve got a problem exiting a thread in the right way. 我在以正确的方式退出线程时遇到问题。 I adapted some code that helps me running a executable in a Commandline window from my code. 我改编了一些代码,这些代码可帮助我从代码中在命令行窗口中运行可执行文件。 Until this the code works fine. 在此之前,代码可以正常工作。 The problem is that after the program that I´m calling is finished and returns to command line, one thread keeps running, How do I get the information which one is finished and how do I kill it afterwards properly? 问题是我调用的程序完成并返回命令行后,一个线程保持运行状态,如何获取完成的信息,以及如何正确销毁该信息? I already tried several options with wait and notify but I couldn´t get them to work. 我已经尝试过等待通知的几种方法,但是我无法使它们起作用。 Thanks for the help, that´s my code: 感谢您的帮助,这是我的代码:

public class InnExec {

    String wkdir;
    static String ActCommand;
    static String InFile;



    InnExec(String wkdir, String ActCommand, String InFile) {
        InnExec.ActCommand=ActCommand;
        this.wkdir=wkdir;
        InnExec.InFile=InFile;
    }
    public Process Run() throws IOException, InterruptedException {
    File wkdir=new File(this.wkdir);


    ProcessBuilder processBuilder= new ProcessBuilder();
    Boolean Windows=true;
    if (!System.getProperty("os.name").contains("Windows")){
        Windows=false;
    }
    if(Windows){
        processBuilder.command("cmd.exe").redirectErrorStream(true);
    }
    else{
        processBuilder.command("/bin/bash").redirectErrorStream(true);
    }
    processBuilder.directory(wkdir);
      Process process = processBuilder.start();
      Thread processOutputHandlerThread=createAndStartProcessOutputHandlerThread(process);
      Thread processInputHandlerThread =createAndStartProcessInputHandlerThread(process);

  return process;



    }
    private static Thread createAndStartProcessInputHandlerThread(final Process process) {
        Thread thread = new Thread(new Runnable() {
          public void run() {
            PrintWriter printWriter = new PrintWriter(process.getOutputStream());
            //Scanner scanner = new Scanner(System.in);
            try {
                printWriter.println(ActCommand+"<"+InFile);
                printWriter.flush();
               Thread.sleep(50);
              }
             catch (InterruptedException interruptedException) {
                 //
            }

          }
        });
        thread.start();

        return thread;
      }



    private static Thread createAndStartProcessOutputHandlerThread(final Process process) {
        Thread thread = new Thread(new Runnable() {
          public void run() {
            try {

              char c = Character.MAX_VALUE;
              while ((c = (char) process.getInputStream().read()) != Character.MAX_VALUE) {
                  System.out.print(c);

               // Ausgabe auf Java Console

              }

            } catch (IOException e) {
              e.printStackTrace();
            }

          }

        });
        thread.start();

        return thread;
      }



}

You could use CountDownLatch 您可以使用CountDownLatch

In your main method, you could define how many threads you would have, inject it within each of your threads and wait for their completion. 在您的main方法中,您可以定义要拥有的线程数,将其注入每个线程中,然后等待它们完成。 Once a thread completes, its would call countdown api and once all your thread completes, your main method can continue further. 一旦线程完成,它将调用countdown api,一旦所有线程完成,您的main方法就可以继续。

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

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