2

IPアドレスにpingを実行するクラスがあります。pingを開始するには、pingを開始するためのpublic vod run()メソッドがあります。問題は、同時により多くのIPアドレスにpingを実行したいということです(IPアドレスごとに新しいスレッドが必要です)。では、forループ内に新しいスレッドを作成するにはどうすればよいですか。これが私のpingクラスのコードです:

public void run()
    {
    if (dbConnection.ConnectToDB())
    {           
        for (;GateWayKey<=GateWayKeyStop;GateWayKey++)
        {
            if(stop || this.isInterrupted()){
                return;
            }
            ip="192.168."+GateWayKey+".1";
            InetAddress address;
            try {
                address = InetAddress.getByName(ip);
                try {

                    if (address.isReachable(PingTime))
                    {

                        //System.out.println("Pronaden GateWay: "+ip)
                    //  labele.IP
                        sql="INSERT INTO `iptables` (`IP` , `COMPUTER_NAME` , `GATEWAY_KEY`) VALUES ('"+ip+"', '"+address.getHostName().toString()+"', '"+GateWayKey+"');";


                        framedocs.WriteMonitorData (ip, address.getHostName().toString(),"2000","DA",dbConnection.WriteToDB(sql));
                        for (;SubNetKey<=SubNetKeyStop;SubNetKey++)
                        {
                            if(stop || this.isInterrupted()){
                            return;
                            }
                            InetAddress addressIps = InetAddress.getByName("192.168."+GateWayKey+"."+SubNetKey);
                            System.out.println("Provjeravam IP: "+addressIps);
                            if (addressIps.isReachable(PingTime))
                            {
                                ip="192.168."+GateWayKey+"."+SubNetKey;
                                System.out.println("Pronaden IP: "+ip);
                                sql="INSERT INTO `iptables` (`IP` , `COMPUTER_NAME` , `GATEWAY_KEY`) VALUES ('"+ip+"', '"+addressIps.getHostName().toString()+"', '"+GateWayKey+"');";
                                framedocs.WriteMonitorData (ip, address.getHostName().toString(),"2000","DA",dbConnection.WriteToDB(sql));                          
                            }
                            else
                            {
                                framedocs.WriteMonitorData (addressIps.toString(), "N/A","2000","NE","N/A");
                            }

                        }
                    }
                    else
                    {
                            framedocs.WriteMonitorData (ip, "N/A","2000","NE","N/A");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    framedocs.WriteMonitorData (ip, "N/A","2000",e.getMessage(),"N/A");
                }
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                ;
                framedocs.WriteMonitorData (ip, "N/A","2000",e.getMessage(),"N/A");
            }
        }

    }
    else
    {
        framedocs.WriteMonitorData ("MySQL error", "N/A","N/A","N/A","N/A");
    }

}

4

2 に答える 2

2

これらの種類のタスクを実行する一般的な方法は、まず、各スレッドから取得する結果を保持するクラスを作成することです。

final class PingResult {
    public String ip;
    public String hostname;
    //... other things you want go here
}

次に、実際の作業を行う callable を作成します

class PingTask extends Callable<PingResult>{
    private final String gateWayKey, subNetKey;
    //... other parameters you need go here
    public Ping( String gwKey, String snKey /*+ others? */ ){
        // This is just a way to pass parameters to your pinging function
        this.gateWayKey = gwKey;
        this.subNetKey = snKey;
        // ...
    }

    public PingResult call(){

        // Do actual pinging work here

        if ( /* Success */ )
        {
            PingResult result = new PingResult();
            result.ip= /*Fill these in*/;
            result.hostname = /* ... */;
            return result;
        }
        // Otherwise we failed, I'm using null as a failure sentinel
        // (you can come up with something better)
        return null;
    }
}

次に、呼び出し元のコードで、スレッド プールを設定し、要求をキューに入れ、結果を処理します。

// Might need to tweak the # for best performance
final int NUM_THREADS = Runtime.getRuntime.availableProcesses(); 
ExecutorService exec = Executors.newFixedThreadPool( NUM_THREADS );

List<Future<PingResult>> results = new ArrayList<PingResult>();

for(/* ... */){
    results.add( exec.submit( new PingTask( gateway, subnet ) ) );
}

for( Future<PingResult> future : results ){
    PingResult result = future.get();

    // Process the result here (this is where you insert into the DB)
}

exec.shutdown(); // VERY IMPORTANT. If you don't do this the JVM will never stop.
于 2012-04-16T16:04:56.137 に答える
0

メインクラス内に新しいクラス
を作成し、新しいスレッドを作成する必要があるたびにその内部クラスで操作を実行しますその内部クラスの新しいインスタンスを開始し、この目的のために作成されたそのメソッドを呼び出す
だけですこの回答が役に立たない場合マルチスレッドに関する他の回答を確認してください。

于 2012-04-16T15:34:13.430 に答える