有没有办法在可调用的方法中进行参数?

DMo*_*DMo 38 java multithreading arguments ping callable

我已经创建了一段代码,它接受一个I​​P地址(来自另一个类中的main方法),然后循环遍历一系列IP地址,并在每个IP地址上执行.我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)

IPtoPing曾经是所采取的论据.

Ste*_*n C 49

您不能将其作为参数传递,call()因为方法签名不允许它.

但是,您可以将其作为构造函数参数传递; 例如

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);
        ....
    }
}
Run Code Online (Sandbox Code Playgroud)

(我纠正了一些令人震惊的代码风格违规!!)

或者,您可以:

  • 将DoPing声明为内部类,并将其引用到final ipToPing封闭范围中的a或

  • 添加setIpToPing(String ipToPing)方法.

(最后一个允许DoPing重用一个对象,但缺点是你需要同步才能安全地访问它.)


Vic*_*kin 7

添加到Jarle的答案 - 如果您创建Callable匿名类的实例,您可以使用final匿名类之外的字段将数据传递到实例:

    final int arg = 64;
    executor.submit(new Callable<Integer>() {
        public Integer call() throws Exception {
            return arg * 2;
        }
    });
Run Code Online (Sandbox Code Playgroud)


Jar*_*sen 5

当您创建doPing类(应该是类名中的captial字母)时,请在构造函数中发送ip-address.在call-method中使用此ip-address.


Per*_*Per 5

你不能传递参数,call()因为方法签名不允许它,但这里至少有一种解决方法

  1. 定义一个包装/实现Callable和的抽象类
  2. 实现一个setter来"注入"结果 call()

定义一个抽象类:

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 ();
}
Run Code Online (Sandbox Code Playgroud)

定义应该触发回调的方法:

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();
}
Run Code Online (Sandbox Code Playgroud)

在您要呼叫的地方iWillFireTheCallback:

定义回调函数(甚至可以在方法内部):

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后iWillFireTheCallback在传递回调时调用:

iWillFireTheCallback(new MyCallback());
Run Code Online (Sandbox Code Playgroud)