在public void method()中的for循环中创建新线程

时间:2012-04-16 15:29:52

标签: java multithreading thread-safety ping void

我有一个ping IP地址的类。要开始ping我有public vod run()方法来开始ping。问题是我想要同时ping更多的IP地址(对于每个IP地址,我需要新的线程)。那么如何在for循环中创建新的Thread。这是我的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");
    }

}

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

在主类中创建一个新类,并在该内部类中执行操作 每次你需要创建一个新线程时,只需启动该内部类的新实例并调用为此目的创建的方法 如果您觉得这个答案没用,请查看我对多线程的其他答案。