2012-04-03 4 views
30

私は、(別のクラスのmainメソッドからの)IPアドレスを受け取り、それが行くにつれてそれぞれにpingする一連のIPアドレスをループするコードを作成しました。私はこれにGUIのフロントエンドがあり、クラッシュしていました(なぜ私はマルチスレッドをやったのですか)。私の問題は、呼び出し可能なコードとして私のpingコードの引数としてIPアドレスを取ることができなくなります。これを行う方法を見つけることができないようです。呼び出し可能なメソッドが引数を取る方法はありますか?そうでない場合は、私がしようとしていることを達成するための方法はありませんか?呼び出し可能なメソッドで引数を取る方法はありますか?

コード:

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

あなたが渡すことはできません。

+0

ので....これは動作しません。引数の配列に対して渡されました... – Michael

+0

これはステートメントまたは質問として意図されていますか?いずれにしても、あなたが何を言っているのかわからない。 –

5

あなたは(クラス名でキャピタル・文字でなければなりません)ドーピング・クラスを作成すると、目に送ります。 eコンストラクタのIPアドレス。このip-addressをcall-methodで使用します。

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

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

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

あなたdoPingクラスの一部(final)フィールド、およびそれらを初期化するコンストラクタを入れ、その後、あなたはdoPingのコンストラクタにcall()に使用する値を渡しますメソッドシグネチャでは許可されていないため、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オブジェクトを再利用することができますが、欠点は、あなたがスレッド安全にアクセスするために同期させる必要がありますということです。)Jarleの答えに追加

6

- 場合には、あなたが作成しますCallable匿名クラスのインスタンスとして、あなたはインスタンスにデータを渡すための匿名クラスの外でfinalフィールドを使用することができます。

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

あなたはこのようなipAddressとしての特性をdefienする必要があり、そのアクセサが満たさhod。その値をconstructorまたはsetterメソッドに渡します。 クラスでは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

メソッドのシグネチャがそれを許すが、ここで

  1. で/実装をラップする抽象クラスを定義することを回避するために、少なくとも1つの方法ですされませんので、あなたがcall()に引数を渡すことはできませんcall()

に結果を "注入" するセッターを実装 Callable
  • :あなたはiWillFireTheCallbackを呼び出したい場所で

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

    :コールバックを起動するメソッドを定義し

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

    0は、抽象クラスを定義します。

    コールバック関数を定義する(メソッド内でも可能):

    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()); 
    
  • 関連する問題