有没有办法在可调用的方法中获取参数?
2022-08-31 22:26:18
我创建了一段代码,它采用一个IP地址(来自另一个类中的main方法),然后循环通过一系列IP地址,在进行ping时对每个IP地址进行ping。我有一个GUI前端,它崩溃了(这就是为什么我做多线程。我的问题是,我不能再将IP地址作为ping代码中的参数作为其可调用的。我到处寻找这个,似乎找不到绕过这个问题的方法。有没有办法让可调用的方法获取参数?如果没有,有没有其他方法可以完成我想要做的事情?
我的代码示例:
public class doPing implements Callable<String>{
public String call() throws Exception{
String pingOutput = null;
//gets IP address and places into new IP object
InetAddress IPAddress = InetAddress.getByName(IPtoPing);
//finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set.
//Results can vary depending on permissions so cmd method of doing this has also been added as backup
boolean reachable = IPAddress.isReachable(1400);
if (reachable){
pingOutput = IPtoPing + " is reachable.\n";
}else{
//runs ping command once on the IP address in CMD
Process ping = Runtime.getRuntime().exec("ping " + IPtoPing + " -n 1 -w 300");
//reads input from command line
BufferedReader in = new BufferedReader(new InputStreamReader(ping.getInputStream()));
String line;
int lineCount = 0;
while ((line = in.readLine()) != null) {
//increase line count to find part of command prompt output that we want
lineCount++;
//when line count is 3 print result
if (lineCount == 3){
pingOutput = "Ping to " + IPtoPing + ": " + line + "\n";
}
}
}
return pingOutput;
}
}
IPtoPing曾经是被采取的论点。