Java - Object运行一个外部进程:如何阻止它?

时间:2017-07-04 08:12:34

标签: java

我有一个Java类。它的构造函数调用ProcessBuilder类来运行一个外部问题(下面提供的完整类代码,我认为对我的问题没什么重要的,但我只是为了说明并使我的问题不那么抽象)。 / p>

我的代码效果很好,但是,当一切都完成后,它就不会停止。这对我来说似乎合乎逻辑,因为外部进程仍在运行。所以我找到了如何阻止它的方法。由于我的类刚启动它,我认为类实例和正在运行的外部进程之间没有“链接”,所以如果我销毁这个对象,外部进程仍然会运行。我的问题是:我怎样才能杀死那个外部过程?

我很抱歉有一个愚蠢的问题,必须在某个地方回答,但我找不到。

非常感谢你的时间!

public static class OptimizerMOEA extends ExternalProblem {

    public static final String SEPARATOR = "\\s+"; //a space
    public static final float LOWER_BOUND = 0.0;
    public static final float UPPER_BOUND = 275000000*3;
    public static final String CPP_EXE_NAME = "optimizer_moea";

    public OptimizerMOEA() throws IOException{
        super(CPP_EXE_NAME);
    }

    public OptimizerMOEA(String path) throws IOException{
        super(path);
    }

    @Override
    public String getName() {
        return "OptimizerMOEA";
    }

    @Override
    public int getNumberOfVariables() {
        //!!! TODO: pass this value from C++ function to this class
       return 6890;
    }

    @Override
    public int getNumberOfObjectives() {
       return 2;
    }

    @Override
    public int getNumberOfConstraints() {
        return 0;
    }

    @Override
    public Solution newSolution() {
        Solution solution = new Solution(getNumberOfVariables(), 
        getNumberOfObjectives());


        for (int i = 0; i < getNumberOfVariables(); i++) {
            solution.setVariable(i, new RealVariable(LOWER_BOUND, UPPER_BOUND));
        } 


        return solution;
    }



}//EOF OptimizerMOEA class

编辑:

这是我使用的父类的构造函数:

/**
 * Constructs an external problem using {@code new
 * ProcessBuilder(command).start()}.  If the command contains arguments,
 * the arguments should be passed in as separate strings, such as
 * <pre>
 *   new ExternalProblem("command", "arg1", "arg2");
 * </pre>
 * 
 * @param command a specified system command
 * @throws IOException if an I/O error occured
 */
public ExternalProblem(String... command) throws IOException {
    this(new ProcessBuilder(command).start());
}

这些是其他的构造者。我的Java类通过标准I / O与外部进程通信。所以使用套接字的构造函数只是为了完整性。

使用构造函数和参数中的进程可能是一个解决方案。在这种情况下,我仍然感兴趣,如果有一种方法来解决第一个构造函数的问题(只是因为我没有看到它,发现它是一个有趣的问题,我想知道在哪种情况下这个构造函数可能是有用的,为什么)。

/**
 * Constructs an external problem that connects to a remote process via
 * sockets.  The remote process should be instantiated and already
 * listening to the designated port number prior to invoking this 
 * constructor.
 * 
 * @param host the host name of the remote system; or {@code null} to use
 *        the local host
 * @param port the port number
 * @throws UnknownHostException if the IP address of the specified host
 *         could not be determined
 * @throws IOException if an I/O error occurred
 */
public ExternalProblem(String host, int port) throws IOException, 
UnknownHostException {
    this(new Socket(host, port));
}

/**
 * Constructs an external problem that connects to a remote process via
 * sockets.  The remote process should be instantiated and already
 * listening to the designated port number prior to invoking this
 * constructor.
 * 
 * @param address the IP address of the remote system
 * @param port the port number
 * @throws IOException if an I/O error occurred
 */
public ExternalProblem(InetAddress address, int port) throws IOException {
    this(new Socket(address, port));
}

/**
 * Constructs an external problem using the specified socket.
 * 
 * @param socket the socket used to send solutions to be evaluated
 * @throws IOException if an I/O error occurred
 */
ExternalProblem(Socket socket) throws IOException {
    this(socket.getInputStream(), socket.getOutputStream());
}

/**
 * Constructs an external problem using the specified process.
 * 
 * @param process the process used to evaluate solutions
 */
ExternalProblem(Process process) {
    this(process.getInputStream(), process.getOutputStream());
    RedirectStream.redirect(process.getErrorStream(), System.err);
}

/**
 * Constructs an external problem using the specified input and output 
 * streams.
 * 
 * @param input the input stream
 * @param output the output stream
 */
ExternalProblem(InputStream input, OutputStream output) {
    super();
    reader = new BufferedReader(new InputStreamReader(input));
    writer = new BufferedWriter(new OutputStreamWriter(output));
}

1 个答案:

答案 0 :(得分:2)

此:

new ProcessBuilder(command).start();

返回Process个对象,如果你持有对它的引用,可以稍后再调用Process.destroy()this SO answer详细说明destroy()实现)