调用“时无法存储矢量数据”;调用;Java中的方法
我的代码有问题。代码将要查找网关/子网,若程序找到一个,它将返回给一个名为“call()”方法的类。这部分工作正常,但问题是我想传递网关的ID(您知道,如果网关是192.168.1。1,它还将把数字1传递给填充已建立网关向量的类)。问题是,出于某种原因,保存网关ID的向量是空的。你能给我一个解决问题的线索吗?顺致敬意, 以下是我在项目中使用的代码:调用“时无法存储矢量数据”;调用;Java中的方法,java,vector,threadpool,runnable,Java,Vector,Threadpool,Runnable,我的代码有问题。代码将要查找网关/子网,若程序找到一个,它将返回给一个名为“call()”方法的类。这部分工作正常,但问题是我想传递网关的ID(您知道,如果网关是192.168.1。1,它还将把数字1传递给填充已建立网关向量的类)。问题是,出于某种原因,保存网关ID的向量是空的。你能给我一个解决问题的线索吗?顺致敬意, 以下是我在项目中使用的代码: int GateWayKey = 1; int GateWayKeyStop=254; String ip=""; StoredGW Founded
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();
}
int网关密钥=1;
int GatewayKeytop=254;
字符串ip=“”;
StoredGW FoundedGW=新的StoredGW();
int SubNetKey=2;
int subnetkeystp=254;
Vector AllGateWays=新向量();
向量AllSQLs=新向量();
final int NUM_THREADS=Runtime.getRuntime().availableProcessors();
ExecutorService exec=Executors.newFixedThreadPool(NUM_线程);
公开募捐{
对于(;GateWayKey而言,问题在于:
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;
}
}
}
作为一个不相关的补充说明,您需要看一下,这将使您的代码更容易让其他人理解。问题在于:
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;
}
}
}
作为一个不相关的补充说明,您需要查看,这将使您的代码更容易让其他人理解。+1对于约定:)我想添加向量是同步的。如果不需要,请使用列表。+1对于约定:)我想添加向量是同步的。如果不需要,请使用列表。
AllSQLs.add(exec.submit((new PingTask(ip,GateWayKey, FoundedGW))));