不止一次使用相同的过程

时间:2014-04-29 15:35:09

标签: java batch-file

是否有任何可能的方法来使用相同的进程来运行不同的命令? ,而不是每次都创建新的流程?

public static void removeAlphaChars() {
    try {
    String[] values = { "a", "12", "gbdasg", "asfwqer", "3", "5", "6", "7",
            "8", "9", "0", "-", "123" };
    ProcessBuilder builder = new ProcessBuilder();
    String[] start = { "cmd", "/C"};
    builder.command(start);
    Process p = builder.start();


    for (int i = 0; i < values.length; i++) {
        String[] arg = { "cmd", "/C", "echo.bat", values[i] };

            builder.command(arg);
            p=builder.start(); //<<<<<<< THIS STARTS NEW PROCESS
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            p.waitFor();

            // System.out.println(p2.exitValue());
        } 

    }

    catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

作为builder.start,启动消耗内存的新进程,如何保存我的资源?

0 个答案:

没有答案