How about doing without ProcessBuilder
as suggested by others also.
I have three classes - PingParallel
is my main class, PingTask
is the task performed by each thread, and PingResult
is having result code (we can add some more info also, status message etc.).
PingParallel
package com.test.thread;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class PingParallel {
public static void main(String[] args) {
int totalIps = 89;
ExecutorService executor = Executors.newFixedThreadPool(totalIps);
List<Future<PingResult>> list = new ArrayList<Future<PingResult>>();
Callable<PingResult> callable = null;
for(int i=0; i< totalIps; i++){
callable = new PingTask("127.0.0"+i); // Get the ipAddres buttons[i].getText());
Future<PingResult> future = executor.submit(callable);
list.add(future);
}
for(Future<PingResult> fut : list){
try {
System.out.println(new Date()+ "::"+fut.get());
} catch (Exception e) {
e.printStackTrace();
}
}
executor.shutdown();
}
}
PingTask
package com.test.thread;
import java.net.InetAddress;
import java.util.concurrent.Callable;
public class PingTask implements Callable<PingResult> {
private String ipAddress;
public PingTask(String ipAddress) {
this.ipAddress = ipAddress;
}
@Override
public PingResult call() {
InetAddress inet = null;
try {
inet = InetAddress.getByName(ipAddress);
int resultCode = inet.isReachable(5000) ? 0 : -1;
return new PingResult(ipAddress, resultCode);
} catch (Exception e) {
e.printStackTrace();
return new PingResult(ipAddress, -1);
}
}
}
PingResult
package com.test.thread;
public class PingResult {
private String ipAddress;
private int resultCode;
public PingResult(String ipAddress, int resultCode) {
this.ipAddress = ipAddress;
this.resultCode = resultCode;
}
public String getIpAddress() {
return ipAddress;
}
public int getResultCode() {
return resultCode;
}
public String toString() {
return "IpAddress :: "+ ipAddress + " Result Code : "+ resultCode;
}
}