final的使用问题

孤独的狼2021 2013-03-11 12:05:32

public static List<String> getVerificatedIps(String[] serverIps) {
// 需要返回的List<String> verificatedIps
final List<String> verificatedIps = new ArrayList<String>();
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(50, 60, 60,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50),
new ThreadPoolExecutor.CallerRunsPolicy());
long startTime = System.currentTimeMillis();
final int maxCount = 4;

for (final String ip : serverIps) {
executorPool.execute(new Runnable() {
public void run() {
Integer countSucce = doPingCmd(ip, maxCount);
if (null != countSucce) {
System.out.println("host:[ " + ip + " ] ping cout: "
+ maxCount + " success: " + countSucce);
// 如果ping成功 则把该IP存到一个List里面
// 但是verificatedIps是final的。应该这么做呢
verificatedIps.add(ip);
} else {
System.out
.println("host:[ " + ip + " ] ping cout null");
}
}
});
}
while (executorPool.getActiveCount() > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("complete ping jobs count = " + serverIps.length
+ " , total used time(ms) = "
+ (System.currentTimeMillis() - startTime));
executorPool.shutdown();
return verificatedIps;
}
...全文
176 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvzg_005 2013-03-11
  • 打赏
  • 举报
回复
for循环内部定义一个string,内部处理好的ips,赋值给此string,然后传给外面的String,然后分析字符串
zxj828282 2013-03-11
  • 打赏
  • 举报
回复
top头 top
孤独的狼2021 2013-03-11
  • 打赏
  • 举报
回复
引用 3 楼 flagiris 的回复:
把verificatedIps定义成类全局变量试试呢。。。
这样是可以的。。。但是不想用全局
孤独的狼2021 2013-03-11
  • 打赏
  • 举报
回复
引用 1 楼 lvzg_005 的回复:
for循环内部定义一个string,内部处理好的ips,赋值给此string,然后传给外面的String,然后分析字符串
final 能 add
菖蒲老先生 2013-03-11
  • 打赏
  • 举报
回复
把verificatedIps定义成类全局变量试试呢。。。
  • 打赏
  • 举报
回复
但是verificatedIps是final的。应该这么做呢,final你add值又不影响

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧