为什么在这段代码中ProcessBuilder执行会影响主线程?

时间:2014-07-07 19:21:04

标签: java multithreading performance exec processbuilder

在此程序中,主线程10分钟后,开始时间和当前时间之间的差异增加到1秒以上,最多8秒。为什么这会影响主线程执行的进程生成器。

java版“1.6.0_26”

Java(TM)SE运行时环境(版本1.6.0_26-b03)

Java HotSpot(TM)服务器VM(版本20.1-b02,混合模式

OS: Solaris 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;


public class ExecTest {

        public ExecThread getExecThread(String cmd, int callNumberStart) {
                return new ExecThread(cmd, callNumberStart);
        }

        public class ExecThread implements Runnable {

                private String cmd;
                private int callNumberStart;

                public ExecThread(String cmd, int callNumberStart) {
                        this.cmd = cmd;
                        this.callNumberStart = callNumberStart;
                }

                public String executeCommand(String origLegCallId, String cmd) {
                        Process process = null;
                        String cmdOutput = null;
                        try {
                                List<String> command = new ArrayList<String>();
                                String[] splittedCmd = cmd.split("\\s+");
                                for(String tmp : splittedCmd){
                                        command.add(tmp);
                                }
                                ProcessBuilder builder = new ProcessBuilder(command);
                                process = builder.start();
                                process.waitFor();
                                InputStreamReader isr = new InputStreamReader(process.getInputStream());
                                BufferedReader br = new BufferedReader(isr);
                                String line = null;
                                cmdOutput = new String();
                                while ((line = br.readLine()) != null) {
                                        cmdOutput = cmdOutput + line;
                                }
                        } catch (Exception ex) {
                                ex.printStackTrace();
                                cmdOutput = null;
                        }
                        return cmdOutput;
                }


                @Override
                public void run() {
                        while (true) {
                                System.out.println(callNumberStart + ":: " + Thread.currentThread().getName() + ":: Start " +
 new Date());
                                String output=executeCommand("" + callNumberStart, cmd);
                                System.out.println(Thread.currentThread().getName()+"Output is:"+output);
                                System.out.println(callNumberStart + ":: " + Thread.currentThread().getName() + ":: End " + n
ew Date());
                                callNumberStart++;
                        }

                }

        }

        public static void main (String args []) {
                int numberOfThread =100;
                String cmd = /home/run.sh; //{sleep 5;echo 'hello'}
                ExecutorService es = Executors.newFixedThreadPool(numberOfThread);

                ExecTest test=new ExecTest();

                for (int i = 1; i <= numberOfThread; i++) {
                        es.submit(test.getExecThread(cmd, i*10000));
                }

                while (true) {

                        long startTime = System.currentTimeMillis();
                        for (int j=0; j<5; j++) {
                                try {
                                        Thread.sleep(200);
                                } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                        System.out.println("Processing Time:: " + (System.currentTimeMillis()-startTime));
                }

        }

}

1 个答案:

答案 0 :(得分:1)

由于这个process.waitFor();

查看javadoc here

编辑1:

在第二次查看代码后,您似乎并未关闭流,因此非常快地消耗了大量资源(许多线程在执行IO)。