Sunday, 15 September 2013

Wired behavior of ExecutorService

Wired behavior of ExecutorService

I have application that pings several IPs at same time. To do that I used
ExecutorService to create threads. Problem is when I call function that is
performing pinging, application ping twice same IP address. When I call
PING function first time, all IPs are pinged correctly and program works
fine but problem is when I submit PINGing again. On second testing
(pinging) each IP address is pinged twice. Here is code that I am using
Here I call function PING:
TimeToPing = new PingTime(7000, IPS, maxScans, NUMTHREADS, ttl, this,
InsertedSQLID, ID_IP, 1);
Thread runThread = new Thread(TimeToPing);
TestingISRunningOrNot=true;
runThread.start();
Here is TimeToPing class (that invoke ExecutorService)
public void run(){
String time;
while (CurrentPingNumber<=maxScans){
try {
Thread.sleep(frequency);//frequency is 7000
} catch (InterruptedException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR",JOptionPane.ERROR_MESSAGE);
}
for (String IP : IPS)
exec.submit((new
PingThemAll(IP,ttl,serverGUI,sqlInsertedID,CurrentPingNumber)));
}
CurrentPingNumber++;
}
exec.shutdown();
}
And here is function that PING IPs:
PingThemAll(String IP, int ttl, PingServerGUI PingServerGUI, int
sqlInsertedID, int CurrentPingNumber){
this.IP=IP;
this.ttl=ttl;
pingServerGUI = PingServerGUI;
this.sqlInsertedID=sqlInsertedID;
this.CurrentPingNumber=CurrentPingNumber;
}
public void run(){
InetAddress address;
System.out.println("Pinging: "+IP);
try {
address = InetAddress.getByName(IP);
try {
if (address.isReachable(ttl)) {
pingServerGUI.appendLogs(sdf.format(new Date())+": "+IP +
" reachable\n");
} else {
pingServerGUI.appendLogs(sdf.format(new Date())+":
"+IP + " is not reachabld\n");
}
} catch (IOException e) {
return;
}
} catch (UnknownHostException e) {
return;
}
}
HERE IS PROGRAM OUTPUT:
When I call function for first time:
Pinging 192.168.1.1
Pinging 192.168.111.1
Pinging 192.168.196.1
Pinging 192.168.1.2
And here is output when I call function for second time:
Pinging 192.168.1.1
Pinging 192.168.111.1
Pinging 192.168.196.1
Pinging 192.168.1.2
Pinging 192.168.1.1
Pinging 192.168.111.1
Pinging 192.168.196.1
Pinging 192.168.1.2

No comments:

Post a Comment