java执行bat阻塞的解决方法
java在执行.bat文件时,经常会出现卡住不动的情况,我们称之为阻塞.
发生阻塞的原因是,在使用Runtime.getRuntime.exec("xx:/xxxx.bat")时,会有outputStream,ErrStream,InputStream三个IO流,如果这三个IO流都有数据的传输操作,那么必须对每一个IO流进行处理,如果有一个IO流漏处理了,那么程序将无限期的等待该IO流被处理,继而程序阻塞.
下面,已java打开安装apache2.2的.bat文件为例,说明一下如何处理阻塞.
package com.multiable.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.CountDownLatch;
public class TestApache {
public static void main(String[] args) {
try {
final CountDownLatch threadSignal = new CountDownLatch(2);//初始化countDown
System.out.println("Start ......");
final Process pro = Runtime.getRuntime().exec("cmd /c \"c:\\start.bat\"");
pro.getOutputStream().close();
Runnable errThread = new Runnable() {
@Override
public void run() {
try {
InputStream is = pro.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";
StringBuilder sb = new StringBuilder();
while((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("error : " + sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
threadSignal.countDown();//线程结束时计数器减1
}
};
new Thread(errThread).start();
Runnable inputThread = new Runnable() {
@Override
public void run() {
try {
InputStream is = pro.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";
StringBuilder sb = new StringBuilder();
while((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("input : " + sb.toString());
is.close();
isr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
threadSignal.countDown();//线程结束时计数器减1
}
};
new Thread(inputThread).start();
try {
threadSignal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("=========================");
Runtime.getRuntime().exec("cmd /c net start apache2.2");
System.out.println("cmd /c net start apache2.2");
} catch (IOException e) {
e.printStackTrace();
}
}
}
关于OutputStream,程序用不到,直接在开始的时候就应该close掉.对于errStream & inputStream应该分别用一个线程来读取出IO流中的内容.
注意:必须使用线程,否则依然会阻塞.
final CountDownLatch threadSignal = new CountDownLatch(2);//初始化countDown
这一个栏位是用来让主线程等待两个子线程执行完毕用的.