问题
I have created a piece of code which takes an IP address (from main method in another class) and then loops through a range of IP addresses pinging each one as it goes. I have a GUI front end on this and it was crashing (hence why I've done the multithreading. My problem is I can no longer take the IP address as an argument in my ping code as its callable. I've searched all over for this and cant seem to find a way to get round this. Is there a way for a callable method to take arguments? If not is there any other way to accomplish what I'm trying to do?
sample of my code:
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 used to be the argument that was taken.
回答1:
You can't pass it as the argument to call()
because the method signature doesn't allow it.
However, you can pass it as a constructor argument; e.g.
public class DoPing implements Callable<String>{
private final String ipToPing;
public DoPing(String ipToPing) {
this.ipToPing = ipToPing;
}
public String call() throws SomeException {
InetAddress ipAddress = InetAddress.getByName(ipToPing);
....
}
}
(I've corrected a couple of egregious code style violations!!)
Alternatively, you could:
declare DoPing as an inner class and have it refer to a
final ipToPing
in the enclosing scope, oradd a
setIpToPing(String ipToPing)
method.
(The last allows a DoPing
object to be reused, but the downside is that you will need to synchronize to access it thread-safely.)
回答2:
Adding to Jarle's answer -- in case you create Callable
as instance of anonymous class, you can use final
field outside of anonymous class for passing data into the instance:
final int arg = 64;
executor.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return arg * 2;
}
});
回答3:
When you create the doPing-class (should be captial letter in class name), send in the ip-address in the constructor. Use this ip-address in the call-method.
回答4:
You can't pass arguments to call()
because the method signature doesn't allow it but here is at least one way to work around that by
- defining an abstract class that wraps/implements
Callable
and - implementing a setter to "inject" a result into
call()
Define an abstract class:
import java.util.concurrent.Callable;
public abstract class Callback<T> implements Callable<Void> {
T result;
void setResult (T result) {
this.result = result;
}
public abstract Void call ();
}
Define the method that should fire the callback:
public void iWillFireTheCallback (Callback callback) {
// You could also specify the signature like so:
// Callback<Type of result> callback
// make some information ("the result")
// available to the callback function:
callback.setResult("Some result");
// fire the callback:
callback.call();
}
In the place where you want to call iWillFireTheCallback
:
Define the callback function (even possible inside methods):
class MyCallback extends Callback {
@Override
public Void call () {
// this is the actual callback function
// the result variable is available right away:
Log.d("Callback", "The result is: " + result);
return null;
}
}
And then call iWillFireTheCallback
while passing in the callback:
iWillFireTheCallback(new MyCallback());
回答5:
Put some (final
) fields in your doPing
class, and a constructor that initializes them, then pass the values you want to use in call()
to the constructor of doPing
:
public class doPing implements Callable<String> {
private final String ipToPing;
public doPing(String ip) {
this.ipToPing = ip;
}
public String call() {
// use ipToPing
}
}
回答6:
You have to defien a property such as ipAddress
and its accessor method. and passing its value in constructor
or by setter
method. In doPing
class use ipAddress
property.
class DoPing/* In java all classes start with capital letter */implements Callable<String>
{
private String ipAddress;
public String getIpAddress()
{
return ipAddress;
}
public void setIpAddress(String ipAddress)
{
this.ipAddress = ipAddress;
}
/*
* Counstructor
*/
public DoPing(String ipAddress )
{
this.ipAddress = ipAddress;
}
@Override
public String call() throws Exception
{
// your logic
}
}
来源:https://stackoverflow.com/questions/9992992/is-there-a-way-to-take-an-argument-in-a-callable-method