ListeningExecutorService+CountDownLatch+Semaphore附Fping for linux and windows

本文介绍了一个使用Java进行并发编程的例子,通过ListeningExecutorService实现子任务的状态监听,并利用CountDownLatch和Semaphore来控制多线程任务的执行与并发数量。此外,还展示了如何使用fping工具批量检查IP地址的可达性。
摘要由CSDN通过智能技术生成
	ListeningExecutorService  子任务状态结果监听
	CountDownLatch 计数器 多线程执行子任务用
	Semaphore 信号量 控制并发


    private static ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));

    private static ListeningExecutorService resultPool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));

    private static Semaphore available = null;


    private Set<String> exec(Set<String> ips) throws InterruptedException {
        available = new Semaphore(20, true);

        CountDownLatch latch = new CountDownLatch(ips.size());
        Set<String> result = new HashSet<>();

        for (String ip : ips) {
            available.acquire();
            ListenableFuture<String> submit = listeningExecutorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    String line = null;
                    Runtime runtime = Runtime.getRuntime();
                    try {
                        Process process = null;
                        process = runtime.exec("fping -t 100 -a " + ip);
                        InputStream is = process.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        line = br.readLine();
                        is.close();
                        isr.close();
                        br.close();
                    } catch (Exception e) {
                        throw e;
                    }
                    return line;
                }
            });

            FutureCallback<String> futureCallback = new FutureCallback<String>() {
                @Override
                public void onSuccess(String ip) {
                    available.release();
                    latch.countDown();
                    if(null!=ip){
                        result.add(ip);
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    available.release();
                    latch.countDown();
                }
            };
            Futures.addCallback(submit, futureCallback, resultPool);

        }
        latch.await();
        return result;
    }

完整的 fping

//     linux
//    fping  -t 100 -a -g 100.28.218.3 100.28.218.255
//  fping  -t 100 -u -g 100.28.218.3 100.28.218.255
//    fping -t 100 100.28.218.3 100.28.218.255
//-a  :显示存活的(alive)
//-g : 指定一个范围 (group)
//-u : 显示不存活的(unreacheable)。和-a 是取反的效果
// -r 请求次数。默认三次


connectIpList("100.26.72.1","100.26.72.1.250");

 public static Set<String> connectIpList( String start, String end, Set<String> ips) {
        Set<String> connect = new HashSet<>();
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec("fping -g -a -t 100 " + start + " " + end);
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                connect.add(line);
            }
            InputStream er = process.getErrorStream();
            InputStreamReader err = new InputStreamReader(er, "GBK");
            BufferedReader ebr = new BufferedReader(err);
            String eine = null;
            StringBuffer esb = new StringBuffer();
            while ((eine = ebr.readLine()) != null) {
                esb.append(eine);
            }
            if (esb.length() > 5) {
                logger.error(" ip fping 异常 >>" + esb.toString());
            }
            is.close();
            isr.close();
            br.close();
        } catch (IOException e) {
            logger.error(" ip fping 异常 >>" + e);
        }
        return connect;
    }



//  Windows   
//-w 应答超时时间
// -r 请求次数。默认三次
//-t 每次fping的间隔时间
// -g 范围 startIP/endIP 
//-T 打印每次ping完成是时间戳
//-D 打印日期



  public static Set<String> execWindows(String start, String end) {
        Set<String> connect = new HashSet<>();
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec("fping -g " + start + "/" + end+" -w 100") ;
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                if(line.startsWith("Reply")){
                    Pattern pattern = Pattern.compile("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
                    Matcher matcher = pattern.matcher(line );
                    while (matcher.find()) {
                        String ip = matcher.group(0);
                        connect.add(ip);
                    }
                }
            }
            is.close();
            isr.close();
            br.close();
        } catch (IOException e) {
            System.out.println("自动上架 ip fping 异常 >>" + e);
        }
        return connect;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值