2012-04-03 133 views
30

我已经创建了一段代码,它需要一个IP地址(来自另一个类中的main方法),然后循环遍历每个IP地址的IP地址范围。我有一个图形用户界面的前端,这是崩溃(因此,为什么我已经完成了多线程。我的问题是我不能再将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曾经是拍摄参数

回答

37

你不可错过它作为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); 
     .... 
    } 
} 

(我已经纠正了几个令人震惊的代码风格的侵犯!)

或者,您可以:

  • 声明兴奋剂作为一个内部类,并让它指final ipToPing

  • 添加setIpToPing(String ipToPing)方法。

(最后允许DoPing对象被重复使用,但缺点是,你将需要同步到线程安全存取)

+0

所以....如果你通过的可赎回作为参数传递给它试图迭代可调用另一个函数,这将不起作用针对它通过的一系列参数... – Michael 2015-09-12 20:54:21

+0

这是作为陈述或问题的目的吗?无论哪种方式,我都搞不清你在说什么/问。 – 2015-09-12 23:32:18

5

当您创建掺杂级(应该在类名captial字母),发送在日e-ip地址在构造函数中。在调用方法中使用此ip地址。

4

把你doPing类中的一些(final)领域,将它们初始化一个构造函数,然后传递要在call()使用到的doPing构造函数的值:

public class doPing implements Callable<String> { 
    private final String ipToPing; 

    public doPing(String ip) { 
     this.ipToPing = ip; 
    } 

    public String call() { 
     // use ipToPing 
    } 
} 
6

添加到Jarle的答案 - 如果你创建Callable为匿名类的实例,你可以使用final场匿名类之外的数据传给实例:

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

你必须defien这样的财产ipAddress及其访问会见HOD。并通过constructorsetter方法传递其值。在doPing类中使用ipAddress属性。

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 
    } 
} 
2

你不能传递参数call()因为方法签名不允许,但在这里是要解决由

  1. 定义一个封装/实现了一个抽象类至少有一种方式Callable
  2. 实现设置器, “注入” 的结果为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(); 
} 

定义应该解雇回调方法:

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(); 
} 

在要拨打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; 
    } 
} 

然后调用iWillFireTheCallback而传入回调:

iWillFireTheCallback(new MyCallback()); 
相关问题