PING & MULTITHREADING
Hi all, I know that "ping"related questions have been answered a million times but i wanted an opinion on something i am working on rite now.
I need to ping 16 diff IP Addresses every 4 secs, get the respnonse and refresh a SWING GUI. I am using runtime.exec for the ping portion as well as Timer Class for implementing the 4 secs requirement. But the time taken to ping all the IP Addresses is more than 4 seconds and the ping function for this is only called once the entire method has been executed. I have minimised the timeout option for "ping" in an attempt to make the ping faster but...
Can anyone suggest a better way of doing this? Can I ping all 16 IP Addresses and start another round in 4 secs? Could some one pls advise. I would really appreciate this.
Thansk a lot
[811 byte] By [
swg1] at [2007-9-26 3:13:55]

Thanks for replying. I have managed to implement some form of multi threading wherein I have created 8 threads, each thread will ping 2 IP Addresses.
I would appreciate if you could advise me as to how i can go about storing the outcome of each thread into a vector or a array etc.
I desperately need an answer or a suggestion for this.
Thanks in advance.
I am pasting my source code, hope it helps in making my problem clearer..
.............................................................................
import java.util.*;
import java.lang.*;
public class TwoThreadsDemo {
public void printingAll(){
SimpleThread s9 = new SimpleThread();
s9.setPriority(Thread.MIN_PRIORITY);
s9.printAll();
}
public static void main (String[] args) {
final java.util.Vector tmp = new java.util.Vector(10);
int xx=1;
HAIHLR ha1 = new HAIHLR("HA_iHLR1", "IP1", "IP2");
HAIHLR ha3 = new HAIHLR("HA_iHLR2", "IP1", "IP2");
HAIHLR ha4 = new HAIHLR("HA_iHLR3", "IP1", "IP2");
HAIHLR ha10 = new HAIHLR("HA_iHLR4", "IP1", "IP2");
HAIHLR ha6 = new HAIHLR("HA_iHLR5", "IP1", "IP2");
HAIHLR ha7 = new HAIHLR("HA_iHLR6", "IP1", "IP2");
HAIHLR ha8 = new HAIHLR("HA_iHLR7", "IP1", "IP2");
HAIHLR ha9 = new HAIHLR("HA_iHLR8", "IP1", "IP2");
tmp.addElement(ha1);
tmp.addElement(ha3);
tmp.addElement(ha4);
tmp.addElement(ha10);
tmp.addElement(ha6);
tmp.addElement(ha7);
tmp.addElement(ha8);
tmp.addElement(ha9);
String z=null;
Vector color = new Vector(10);
HAIHLR ha = (HAIHLR)tmp.elementAt(0);
HAIHLR ha11 = (HAIHLR)tmp.elementAt(1);
HAIHLR ha12 = (HAIHLR)tmp.elementAt(2);
HAIHLR ha13 = (HAIHLR)tmp.elementAt(3);
HAIHLR ha14 = (HAIHLR)tmp.elementAt(4);
HAIHLR ha15 = (HAIHLR)tmp.elementAt(5);
HAIHLR ha16 = (HAIHLR)tmp.elementAt(6);
HAIHLR ha17 = (HAIHLR)tmp.elementAt(7);
SimpleThread s1 = new SimpleThread(ha.node1,ha.node2);
SimpleThread s2 = new SimpleThread(ha11.node1,ha11.node2);
SimpleThread s3 = new SimpleThread(ha12.node1,ha12.node2);
SimpleThread s4 = new SimpleThread(ha13.node1,ha13.node2);
SimpleThread s5 = new SimpleThread(ha14.node1,ha14.node2);
SimpleThread s6 = new SimpleThread(ha15.node1,ha15.node2);
SimpleThread s7 = new SimpleThread(ha16.node1,ha15.node2);
SimpleThread s8 = new SimpleThread(ha17.node1,ha15.node2);
s1.start();
s2.start();
s3.start();
s4.start();
s5.start();
s6.start();
s7.start();
s8.start();
TwoThreadsDemo tw = new TwoThreadsDemo();
tw.printingAll();
}//end of main()
}//end of class
.............................................................................
import java.io.*;
import java.lang.*;
import java.util.*;
public class SimpleThread extends Thread {
boolean state1 = false;
boolean state2 = false;
boolean state3 = false;
StringTokenizer st;
String IP1=null;
String IP2=null;
Vector v;
public SimpleThread() {
}
public SimpleThread(String str, String str1) {
this.IP1= str;
this.IP2=str1;
}
public void run() {
try{
Runtime runtime = Runtime.getRuntime();
String cmd = ("/usr/sbin/ping " + " " + IP1 + " " + " 1 ");
String cmd2 = ("/usr/sbin/ping " + " " + IP2 + " " + " 1 ");
System.out.println(cmd);
System.out.println(cmd2);
Process proc = runtime.exec(cmd);
proc.waitFor();
InputStream in = proc.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
String str0 = rd.readLine();
st = new StringTokenizer(str0);
while(st.hasMoreTokens()){
str0 = st.nextToken();
if (str0.equals("alive")){
state2 = true;
}
else {
state2 = false;
}
}//end of while
System.out.println(state2 + getName());
String answer=state2+getName();
v = new Vector(9);
v.addElement(answer);
proc = runtime.exec(cmd2);
proc.waitFor();
in = proc.getInputStream();
rd = new BufferedReader(new InputStreamReader(in));
str0 = rd.readLine();
st = new StringTokenizer(str0);
while(st.hasMoreTokens()){
str0 = st.nextToken();
if (str0.equals("alive")){
state2 = true;
}
else {
state2 = false;
}
}//end of while
System.out.println(state2 + getName());
answer = state2+getName();
v.addElement(answer);
sleep((long)(Math.random() * 100));
}
catch (Exception e) {
}
}
public boolean isDone(){
return state1;
}
public void printAll(){
System.out.println("Hello" + v.elementAt(1));
}
}
swg1 at 2007-6-29 11:23:41 >
