首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java 多线程主机Ip扫描

java 多线程主机Ip扫描

作者头像
葫芦
发布2019-04-17 14:10:45
发布2019-04-17 14:10:45
1.6K0
举报
文章被收录于专栏:葫芦葫芦
代码语言:javascript
复制
1、创建集合用来存所有的ip地址,并指定实现类。
        private Queue<String> allIp;
        allIp = new LinkedList <String>();
        for (int i = mini ; i <= maxi; i++) {
                     String host = hosts + "." + i;
                      allIp.offer( host);
              }
2、创建runnable实现类,创建 getIp()方法每次取一个ip地址测试,synchronized 用以同步处理。
private class rHost implements Runnable {
               private String taskIp = null;
              Message msg = new Message();
               private LinkedList link = new LinkedList();
               @Override
               public void run() {

                      try {
                            while ((taskIp = getIp()) != null) {
                                   if (flag == 1) {
                                         InetAddress addr = InetAddress.getByName(taskIp);
                                          status = (( s - mini) * 100 / ( maxi - mini));
                                          if (addr.isReachable(gettimeout())) {
                      count++;

                                                 link.add(taskIp);
                                         }
                                          handler1.sendEmptyMessage( status);

                                  } else if (flag == 2) {
                                         Thread. currentThread().interrupt();
                                  }

                           }
                            if (!link .isEmpty()) {
                                   msg. obj = link;
                                   pingHandler.sendMessage(msg );
                           }
                     } catch (Exception e) {
                           e.printStackTrace();
                     }

              }
               public String getIp() {
                     String ip = null;

                      synchronized (allIp ) {
                           ip = allIp.poll();
                            s++;
//                         System.out.println(s);
                     }
                      return ip;
              }

3、创建线程池添加上面的runnable接口。
        public void startPing() {
               count = 0;
               int threadNum = 100;
              ExecutorService executor = Executors. newFixedThreadPool(threadNum);
               for (int i = 0; i < threadNum; i++) {
                     executor. execute(new rHost());
              }
              executor.shutdown();
               try {
                      while (!executor.isTerminated()) {
                           Thread. sleep(100);

                     }
              } catch (Exception e) {
                     e.printStackTrace();
              }
       }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014/04/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档