0

我的代码有问题。代码即将找到网关/子网,如果程序找到一个,它会将其返回给一个名为“call()”方法的类。这部分工作正常,但问题是我想传递网关的 ID(你知道网关是否是 192.168.1. 1,它也会将数字1传递给填充已建立网关向量的类)。问题是由于某种原因,包含网关 ID 的向量是空的。你能给我一个线索如何解决问题吗?此致。这是我在项目中使用的代码:

int GateWayKey = 1;
int GateWayKeyStop=254;
String ip="";
StoredGW FoundedGW = new StoredGW();
int SubNetKey = 2;
int SubNetKeyStop = 254;
Vector <Integer> AllGateWays= new Vector <Integer>();
Vector <Future<String>> AllSQLs = new Vector <Future<String>>();

final int NUM_THREADS = Runtime.getRuntime().availableProcessors(); 
ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
public void run() {


    for (;GateWayKey<=GateWayKeyStop;GateWayKey++){
        ip="192.168."+GateWayKey+".1";
        AllSQLs.add(exec.submit((new PingTask(ip,GateWayKey))));
    }
        AllGateWays = FoundedGW.GiveMeGWs();
    for (int j : AllGateWays){
        for (;SubNetKey<=SubNetKeyStop;SubNetKey++){
            ip="192.168."+j+"."+SubNetKey;         
            AllSQLs.add (exec.submit(new PingTask(ip,null))));
        }
 exec.shutdown();
}

这是执行 ping 和存储网关 ID 的类:

public class PingTask implements Callable <String> {
String ips; 
int GateWay;
public PingTask (){
}

public PingTask (String ip, int GateWayKey){
    ips=ip;
    GateWay=GateWayKey;
}

public String call(){
    InetAddress address;
    try {
        address = InetAddress.getByName(ips);
        try {
            if (address.isReachable(5000)) { 
                        StoredGW GWs = new StoredGW();
                        GWs.addNewGW(GateWay);
                } else {
                        return null;
                }
            } catch (IOException e) {
                return null;
            }
        } catch (UnknownHostException e) {
            return null;
        }
}
}

这是我存储网关的课程

public class StoredGW {
Vector <Integer> AllFoundedGWs= new Vector<Integer>();
public void addNewGW(int i){
    AllFoundedGWs.add(i);
}
public Vector<Integer> GiveMeGWs(){
    return AllFoundedGWs;
}

}
4

1 回答 1

3

问题在这里:

StoredGW GWs = new StoredGW();
GWs.addNewGW(GateWay);

您制作了一个新的 StoreGW(作为局部变量),然后将其丢弃。而是使用 , FoundedGW。您必须确保它对您的任务可见,您可能必须将它作为构造函数参数传递,以便它可以在您的任务中使用。

尝试这个:

public class PingTask implements Callable <String> {
    String ips; 
    int GateWay;
    StoredGW store;

    public PingTask (){
    }

    public PingTask (String ip, int GateWayKey, StoredGW store){
        ips=ip;
        GateWay=GateWayKey;
                    this.store = store;
    }

    public String call(){
        InetAddress address;
        try {
            address = InetAddress.getByName(ips);
            try {
                if (address.isReachable(5000)) { 
                            store.addNewGW(GateWay);
                    } else {
                            return null;
                    }
                } catch (IOException e) {
                    return null;
                }
            } catch (UnknownHostException e) {
                return null;
            }
    }
}

然后你可以这样称呼它:

AllSQLs.add(exec.submit((new PingTask(ip,GateWayKey, FoundedGW))));

作为一个不相关的旁注,您需要查看Java 命名约定的标准,它会使您的代码更容易被其他人理解。

于 2013-01-18T12:25:11.060 回答