在此程序中,主线程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));
}
}
}
答案 0 :(得分:1)