问题的提出:
特点:
这里目前只做调用分析:
原来的:
private void runShell(String cmd){
try{
logger.info("the command "+cmd);
// create a process for the shell
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
pb.redirectErrorStream(true); // use this to capture messages sent to stderr
Process shell = pb.start();
InputStream shellIn = shell.getInputStream(); // this captures the output from the command
int shellExitStatus = 0;
try {
shellExitStatus = shell.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
} // wait for the shell to finish and get the return code
// // at this point you can process the output issued by the command
// // for instance, this reads the output and writes it to System.out:
int c;
while ((c = shellIn.read()) != -1) {
logger.info("shell read value:"+c);
}
// close the stream
shellIn.close();
logger.info(" *** End *** "+shellExitStatus);
logger.info("pb command "+pb.command());
logger.info("pb command dir "+pb.directory());
logger.info(pb.environment());
logger.info(System.getenv());
logger.info(System.getProperties());
}
catch (IOException ignoreMe)
{
ignoreMe.printStackTrace();
}
}
修改之后的代码:
//变为成员变量方式
protected volatile Process process;
private void runShell(String cmd) {
try {
logger.info("the command " + cmd);
// create a process for the shell
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
process = pb.start();
final InputStream inputStream = process.getInputStream();
final InputStream errorStream = process.getErrorStream();
new Thread(new Runnable() {
@Override
public void run() {
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line=reader.readLine())!=null){
// logConsole(line);
logger.debug(line);
}
}catch(Exception e){
// log(e);
e.printStackTrace();
logger.debug("接收日志出错,推出日志接收");
}
}
},"one").start();
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(errorStream));
String line;
while((line=reader.readLine())!=null){
// logConsole(line);
logger.debug(line);
}
} catch (Exception e) {
// log(e);
e.printStackTrace();
logger.debug("接收日志出错,推出日志接收");
}
}
},"error").start();
// InputStream shellIn = process.getInputStream(); // this captures the output from the command
int shellExitStatus = 0;
try {
//等待程序结果
shellExitStatus = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
process=null;
}
} catch (IOException ignoreMe) {
ignoreMe.printStackTrace();
}
}
实验结果:
同时处理100条语音。
第一个用时50min。
第二个用时25min。
优势:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章