62,616
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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());
}
}
}