2

我有一个 ping IP 地址的类。要开始 ping,我有公共 vod run() 方法来开始 ping。问题是我想同时 ping 更多的 IP 地址(对于每个 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
}

然后创建一个执行实际工作的可调用对象

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 回答