有没有办法采取一个可调用方法的参数? [英] Is there a way to take an argument in a callable method?

查看:128
本文介绍了有没有办法采取一个可调用方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创造了一项code的这需要一个IP地址(主要方法另一个类),然后通过一个IP地址范围的循环侦测每个人自有其道理。我有一个GUI前端就这个问题和它崩溃(所以为什么我已经做了多线程。我的问题是我不能再取IP地址在我的平code作为其可调用的参数。我有找遍了,这和不能似乎找到一种方法来避开这个,有没有一种方式,可调用的方法来接受参数吗?如果不是有没有其他的方式来完成我想要做什么?

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?

我的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曾经是拍摄参数。

IPtoPing used to be the argument that was taken.

推荐答案

您不能将它作为参数传递给()调用由于该方法签名不允许它。

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 Exception {
        InetAddress ipAddress = InetAddress.getByName(ipToPing);
        ....
    }
}

(我已经纠正了一个令人震惊的夫妇code风格的侵犯!)

(I've corrected a couple of egregious code style violations!!)

另外,你可以:


  • 宣布兴奋剂作为一个内部类,并把它指的是最后ipToPing 在封闭的范围内,或

添加一个 setIpToPing(字符串ipToPing)方法。

(最后允许掺杂对象被重用,但缺点是,你将需要同步线程安全的访问。)

(The last allows a DoPing object to be reused, but the downside is that you will need to synchronize to access it thread-safely.)

这篇关于有没有办法采取一个可调用方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆