我做了一个ping百度的程序,使用的是handler循环。下面是循环体:
private Handler handler = new Handler();
private Runnable task = new Runnable() {
public void run() {
// TODO Auto-generated method stub
handler.postDelayed(this,5000);//设置延迟时间,此处是5秒
//需要执行的代码,pingHost是ping百度的一个类
text.append("\n"+pingHost("www.baidu.com")+count++);
int offset=text.getLineCount()*text.getLineHeight();
if(offset>text.getHeight()){
text.scrollTo(0,offset-text.getHeight());
}
}
};
其中调用的pingHost是:
public String pingHost(String str) {
String resault = "";
try {
// TODO: Hardcoded for now, make it UI configurable
Runtime runtime=Runtime.getRuntime();
Process p = runtime.exec("ping -c 1 -w 1 " + str);
int status = p.waitFor();
if (status == 0) {
// mTextView.setText("success") ;
resault = "success";
} else {
resault = "failed";
// mTextView.setText("fail");
}
} catch (IOException e) {
resault="IOerror";// mTextView.setText("Fail: IOException"+"\n");
} catch (InterruptedException e) {
resault="INTERRUPTerror";
//exit(1);
// mTextView.setText("Fail: InterruptedException"+"\n");
}
finally {
}
return resault;
}
在循环调用312次后,会在ping的时候发生ioexception,无论我简化程序体或者是简化输出的字符串,次数都是这个固定的次数,这是由于主线程阻塞吗?还是由于Linux的ping的设定原因?