在windows上测试java创建的ping进程,报java.io.IOException: Bad file descriptor异常?请教各位是怎么回事?

apinglee 2008-06-12 06:40:06
PingTest.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.lang.*;

public class PingTest
{
public Map collectData(Map commands)
{
Set cmdEntrySet = commands.entrySet();
String[] commandInfos = new String[cmdEntrySet.size()];

int iCmd = 0;
for (Iterator iterator = cmdEntrySet.iterator(); iterator.hasNext(); iCmd++)
{
Map.Entry cmdEntry = (Map.Entry) iterator.next();
String command = (String) cmdEntry.getKey();
commandInfos[iCmd] = command;
}

Hashtable ret = new Hashtable();
for (int i = 0; i < commandInfos.length; i++)
{
Process process = null;
BufferedReader bufferedreader = null;
InputStream errIn = null;
OutputStream outS = null;
Vector data = null;
try
{
process = Runtime.getRuntime().exec(commandInfos[i]);
InputStream inputstream = process.getInputStream();
bufferedreader= new BufferedReader(new InputStreamReader(inputstream));
errIn = process.getErrorStream();
outS = process.getOutputStream();

data = new Vector();
int j = 0;
String str;
StringBuffer sb = new StringBuffer();
while ((str = bufferedreader.readLine()) != null)
{
data.add(str);
j++;
}
}
catch (IOException e) {
System.out.println(e.toString());
}
finally
if( bufferedreader!=null){
try {
bufferedreader.close();
bufferedreader=null;
}
catch (IOException ex) {
// dMsg.warn(ex.getMessage());
}
}else
if(errIn!=null){
try {
errIn.close();
errIn=null;
}
catch (IOException ex) {
// dMsg.warn(ex.getMessage());
}
}else
if(outS!=null){
try {
outS.close();
outS=null;
}
catch (IOException ex) {
// dMsg.warn(ex.getMessage());
}
}
if (process != null) {
process.destroy();
process=null;
}
}
}
ret.put(commandInfos[i], data);
}
return ret;
}
}


PingTest2.java

import java.util.HashMap;
import java.util.Map;

public class PingTest2 {
private static int i=0;
class ThreadTest extends Thread{
public void run(){
PingTest collector = new PingTest();
Map commandsMap = new HashMap();
commandsMap.put("ping 10.75.72.188","");
try {
Map Result = collector.collectData(commandsMap);
System.out.println("Thread"+i+":"+Result.toString());
}
catch (Exception e) {
//dMsg.error("ERROR", e);
System.out.println(e.toString());
}
i++;
}
}

public static void main(String[] argv) {
//MonitorTaskInfo info = new MonitorTaskInfo();
try{
for(int i=0;i<10000;i++){
PingTest2 pp = new PingTest2();
pp.new ThreadTest().start();

Thread.sleep(20);
}
}catch(Exception e){
System.out.println(e.toString());
}


}

}



创建到大概3000个进程左右,便报此异常,还报java.io.IOException: CreateProcess: ping 10.75.72.188 error=1450
这种错误,还报java.lang.NullPointerException空指针。
不知道是不是跟系统资源大小相关,是不是此代码有地方会引起内存泄露?
请路过老大们指点一下,谢谢!!!
...全文
440 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
胡矣 2008-06-12
  • 打赏
  • 举报
回复
产生io异常不知道LZ的网络状况是否良好
java中的内存泄漏请参考
http://blog.csdn.net/yirentianran/archive/2008/06/12/2537985.aspx
不过LZ这样的小程序 一般就是内存泄漏应该也不会引起系统崩溃
apinglee 2008-06-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ybygjy 的回复:]
大致看了一下.. 我认为是线程休眠时出现的问题..
public static void main(String[] argv) {
//MonitorTaskInfo info = new MonitorTaskInfo();
try{
for(int i=0;i <10000;i++){
PingTest2 pp = new PingTest2();
Thread testThread = pp.new ThreadTest().start();

testThread.sleep(20);
}
}catch(Exception e…
[/Quote]

做这个测试实际上就是为了复现一个实际的应用程序中的故障,不知道最终是不是内存泄露造成的呢?
一步一个脚印 2008-06-12
  • 打赏
  • 举报
回复
大致看了一下.. 我认为是线程休眠时出现的问题..
public static void main(String[] argv) {
//MonitorTaskInfo info = new MonitorTaskInfo();
try{
for(int i=0;i<10000;i++){
PingTest2 pp = new PingTest2();
Thread testThread = pp.new ThreadTest().start();

testThread.sleep(20);
}
}catch(Exception e){
System.out.println(e.toString());
}


}

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧